package xyz.luxnk.mxmmod.action;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;

import java.util.Iterator;

public class StormRushAction extends AbstractGameAction {

    private AbstractPlayer player;
    private int cardAmount;

    public StormRushAction(int count) {
        this.actionType = ActionType.CARD_MANIPULATION;
        this.player = AbstractDungeon.player;
        this.duration = Settings.ACTION_DUR_FAST;
        this.cardAmount = count;
    }

    @Override
    public void update() {

        for (int i = 0; i < cardAmount; i++) {

            if (this.duration == Settings.ACTION_DUR_FAST) {
                boolean betterPossible = false;
                boolean possible = false;
                Iterator iterator = this.player.hand.group.iterator();
                while (iterator.hasNext()) {
                    AbstractCard c = (AbstractCard)iterator.next();
                    if (c.type == AbstractCard.CardType.ATTACK) {
                        if (c.costForTurn > 0) {
                            betterPossible = true;
                        } else if (c.cost > 0) {
                            possible = true;
                        }
                    }
                }

                if (betterPossible || possible) {
                    this.findAndModifyCard(betterPossible);
                }
            }
        }

        this.tickDuration();
    }

    private void findAndModifyCard(boolean better) {

        AbstractCard c = this.player.hand.getRandomCard(AbstractCard.CardType.ATTACK, false);
        if (better) {
            if (c.costForTurn > 0) {
                c.costForTurn = 0;
                c.flash();
            } else {
                findAndModifyCard(better);
            }
        } else if (c.cost > 0) {
            c.costForTurn = 0;
            c.flash();
        } else {
            this.findAndModifyCard(better);
        }

    }

}
