package xyz.luxnk.mxmmod.action;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.common.HealAction;
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.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.powers.DexterityPower;
import com.megacrit.cardcrawl.powers.StrengthPower;

public class SadCatAction extends AbstractGameAction {

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

    private AbstractPlayer p;

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

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

    public void checkCardType(AbstractCard card) {
        if (card.type.equals(AbstractCard.CardType.ATTACK)) {
            AbstractDungeon.actionManager.addToBottom(
                    new ApplyPowerAction(
                            p, p, new StrengthPower(p, 1), 1));
        } else if (card.type.equals(AbstractCard.CardType.SKILL)) {
            AbstractDungeon.actionManager.addToBottom(
                    new ApplyPowerAction(
                            p, p, new DexterityPower(p, 1), 1));
        } else if (card.type.equals(AbstractCard.CardType.POWER)) {
            AbstractDungeon.actionManager.addToBottom(
                    new HealAction(
                            p, p, 10));
        } else if (card.type.equals(AbstractCard.CardType.CURSE)) {
            AbstractDungeon.actionManager.addToBottom(
                    new ApplyPowerAction(
                            p, p, new StrengthPower(p, -1), -1));
        } else if (card.type.equals(AbstractCard.CardType.STATUS)) {
            AbstractDungeon.actionManager.addToBottom(
                    new ApplyPowerAction(
                            p, p, new DexterityPower(p, -1), -1));
        }
    }
}
