package xyz.luxnk.mxmmod;

import basemod.BaseMod;
import basemod.ModPanel;
import basemod.interfaces.*;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.evacipated.cardcrawl.modthespire.lib.SpireInitializer;
import com.google.gson.Gson;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.cards.red.*;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.CardHelper;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.localization.*;
import com.megacrit.cardcrawl.powers.AbstractPower;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.unlock.UnlockTracker;
import xyz.luxnk.mxmmod.cards.*;
import xyz.luxnk.mxmmod.characters.Mxm;
import xyz.luxnk.mxmmod.patches.AbstractCardEnum;
import xyz.luxnk.mxmmod.patches.MxmModClassEnum;
import xyz.luxnk.mxmmod.relics.Hamakagami;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;

@SpireInitializer
public class MxmMod implements PostInitializeSubscriber,
        PostCreateStartingDeckSubscriber,
        PostCreateStartingRelicsSubscriber,
        PostExhaustSubscriber,
        PostBattleSubscriber,
        PostDungeonInitializeSubscriber,
        EditCharactersSubscriber,
        EditStringsSubscriber,
        EditRelicsSubscriber,
        EditCardsSubscriber,
        EditKeywordsSubscriber,
        PostDungeonUpdateSubscriber,
        PostPlayerUpdateSubscriber {

    private static final String RELIC_STRING = "localization/mxm_relics-zh.json";
    private static final String CARD_STRING = "localization/mxm_cards-zh.json";
    private static final String POWER_STRING = "localization/mxm_powers-zh.json";
    private static final String KEYWORD_STRING = "localization/mxm_keywords-zh.json";
    private static final String UI_STRING = "localization/mxm_ui-zh.json";

    public static final Color CYAN = CardHelper.getColor(100f, 240f, 245f);

    private static final String MOD_BADGE = "img/UI/badge.png";

    private static final String MXM_BUTTON = "img/charSelect/mxmButton.png";
    private static final String MXM_PORTRAIT = "img/charSelect/mxmPortrait.jpg";

    // 卡片背景图（暂时借用魔理沙小姐的
    private static final String ATTACK_CC = "img/512/bg_attack_mxm_s.png";
    private static final String SKILL_CC = "img/512/bg_skill_mxm_s.png";
    private static final String POWER_CC = "img/512/bg_power_mxm_s.png";
    private static final String ENERGY_ORB_CC = "img/512/cardOrb.png";

    private static final String ATTACK_CC_PORTRAIT = "img/1024/bg_attack_mxm.png";
    private static final String SKILL_CC_PORTRAIT = "img/1024/bg_skill_mxm.png";
    private static final String POWER_CC_PORTRAIT = "img/1024/bg_power_mxm.png";
    private static final String ENERGY_ORB_CC_PORTRAIT = "img/1024/cardOrb.png";

    public static final String CARD_ENERGY_ORB = "img/UI/energyOrb.png";

    public MxmMod() {
        BaseMod.subscribe(this);
        BaseMod.addColor(
                AbstractCardEnum.MXM_COLOR,
                CYAN,
                CYAN,
                CYAN,
                CYAN,
                CYAN,
                CYAN,
                CYAN,
                ATTACK_CC,
                SKILL_CC,
                POWER_CC,
                ENERGY_ORB_CC,
                ATTACK_CC_PORTRAIT,
                SKILL_CC_PORTRAIT,
                POWER_CC_PORTRAIT,
                ENERGY_ORB_CC_PORTRAIT,
                CARD_ENERGY_ORB
        );
    }

    public static void initialize() {
        new MxmMod();
    }

    @Override
    public void receivePostInitialize() {
        ModPanel settingsPanel = new ModPanel();
        Texture badge = ImageMaster.loadImage(MOD_BADGE);
        BaseMod.registerModBadge(
                badge,
                "MxmMod",
                "Luxnk , litter cat",
                "猫小咪来了",
                settingsPanel
        );
    }

    @Override
    public void receiveEditCharacters() {
        BaseMod.addCharacter(
                new Mxm("MaoXiaomi"),
                MXM_BUTTON,
                MXM_PORTRAIT,
                MxmModClassEnum.MXM
        );
    }

    @Override
    public void receiveEditStrings() {

        BaseMod.loadCustomStrings(RelicStrings.class, Gdx.files.internal(RELIC_STRING).readString(
                String.valueOf(StandardCharsets.UTF_8)
        ));
        BaseMod.loadCustomStrings(CardStrings.class, Gdx.files.internal(CARD_STRING).readString(
                String.valueOf(StandardCharsets.UTF_8)
        ));
        BaseMod.loadCustomStrings(PowerStrings.class, Gdx.files.internal(POWER_STRING).readString(
                String.valueOf(StandardCharsets.UTF_8)
        ));
        BaseMod.loadCustomStrings(UIStrings.class, Gdx.files.internal(UI_STRING).readString(
                String.valueOf(StandardCharsets.UTF_8)
        ));

    }

    @Override
    public void receiveEditRelics() {

        //BaseMod.addRelic(new Hamakagami(), RelicType.SHARED);
        BaseMod.addRelicToCustomPool(
                new Hamakagami(),
                AbstractCardEnum.MXM_COLOR
        );
    }

    @Override
    public void receiveEditCards() {

        // 攻击牌
        BaseMod.addCard(new Strike_MXM());
        BaseMod.addCard(new TwinStrike_MXM());
        BaseMod.addCard(new QuickStrike_MXM());
        BaseMod.addCard(new Stab_MXM());
        BaseMod.addCard(new SavageStab_MXM());

        // 技能牌
        BaseMod.addCard(new Defend_MXM());
        BaseMod.addCard(new WarmUp_MXM());
        BaseMod.addCard(new SpeedUp_MXM());
        BaseMod.addCard(new PoorWine_MXM());
        BaseMod.addCard(new SadCat_MXM());
        BaseMod.addCard(new LuckyCat_MXM());

        // 能力牌
        BaseMod.addCard(new Strength_MXM());
        BaseMod.addCard(new Dexterity_MXM());
    }

    @Override
    public void receiveEditKeywords() {
        Gson gson = new Gson();
        Keywords keywords = gson.fromJson(loadJson(KEYWORD_STRING), Keywords.class);
        for (Keyword key : keywords.keywords) {
            BaseMod.addKeyword(key.NAMES, key.DESCRIPTION);
        }
    }

    private static String loadJson(String jsonPath) {
        return Gdx.files.internal(jsonPath).readString(String.valueOf(StandardCharsets.UTF_8));
    }

    @Override
    public void receivePostBattle(AbstractRoom abstractRoom) {
    }

    @Override
    public void receivePostDungeonInitialize() {
    }

    @Override
    public void receivePostExhaust(AbstractCard abstractCard) {
    }

    @Override
    public void receivePostCreateStartingDeck(AbstractPlayer.PlayerClass playerClass, CardGroup cardGroup) {
        if (playerClass == AbstractPlayer.PlayerClass.IRONCLAD) {
            cardGroup.addToTop(new Anger());
        }
    }

    @Override
    public void receivePostCreateStartingRelics(AbstractPlayer.PlayerClass playerClass, ArrayList<String> arrayList) {
        if (playerClass == AbstractPlayer.PlayerClass.IRONCLAD) {
            arrayList.add("Hamakagami");
            UnlockTracker.markRelicAsSeen("Hamakagami");
        }
    }

    @Override
    public void receivePostDungeonUpdate() {
    }

    @Override
    public void receivePostPlayerUpdate() {
        // 抵御诅咒
        if (AbstractDungeon.player.hasRelic("Hamakagami") && AbstractDungeon.player.getRelic("Hamakagami").counter > 0) {
            Iterator deck = AbstractDungeon.player.masterDeck.group.iterator();
            while (deck.hasNext()) {
                AbstractCard c = (AbstractCard)deck.next();
                if (c.color == AbstractCard.CardColor.CURSE) {
                    AbstractDungeon.player.masterDeck.removeCard(c);
                    ((Hamakagami)AbstractDungeon.player.getRelic("Hamakagami")).use();
                    break;
                }
            }
        }
    }

    class Keywords {

        Keyword[] keywords;
    }
}
