package xyz.luxnk.mxmmod.action;

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

public class LuckyCatAction extends AbstractGameAction {

    public static final String[] TEXT = CardCrawlGame.languagePack.getUIString("LuckyCatAction").TEXT;

    private AbstractPlayer p;

    public LuckyCatAction(int amount) {
        this.p = AbstractDungeon.player;
        this.setValues(this.p, AbstractDungeon.player, amount);
        this.actionType = AbstractGameAction.ActionType.CARD_MANIPULATION;
    }

    @Override
    public void update() {
        AbstractCard card;
        if (this.p.drawPile.isEmpty()) {
            this.isDone = true;
            return;
        }
        if (this.p.drawPile.size() == 1) {
            card = this.p.drawPile.group.get(0);
            if (card.cost > 0) {
                card.freeToPlayOnce = true;
            }
            this.p.hand.refreshHandLayout();
            this.isDone = true;
            return;
        }
        if (this.duration == 0.5F) {
            AbstractDungeon.gridSelectScreen.open(this.p.drawPile, this.amount, TEXT[0], false);
            tickDuration();
            return;
        }
        if (AbstractDungeon.gridSelectScreen.selectedCards.size() != 0) {
            for (AbstractCard c : AbstractDungeon.gridSelectScreen.selectedCards) {
                this.p.drawPile.removeCard(c);
                this.p.drawPile.addToTop(c);
                if (c.cost > 0) {
                    c.freeToPlayOnce = true;
                }
                c.unhover();
            }
            AbstractDungeon.gridSelectScreen.selectedCards.clear();
            this.p.hand.refreshHandLayout();
            /*for (AbstractCard c : this.p.exhaustPile.group) {
                c.unhover();
                c.target_x = CardGroup.DISCARD_PILE_X;
                c.target_y = 0.0F;
            }*/
            this.isDone = true;
        }
        tickDuration();
    }
}
