// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2022
//                www.magicbane.com

package engine.net.client.handlers;

import engine.gameManager.DispatchManager;
import engine.mbEnums;
import engine.net.Dispatch;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.RefineMsg;
import engine.net.client.msg.RefinerScreenMsg;
import engine.objects.*;
import engine.server.MBServerStatics;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

public class RefineMsgHandler extends AbstractClientMsgHandler {

    public RefineMsgHandler() {
        super();
    }

    @Override
    protected boolean _handleNetMsg(ClientNetMsg baseMsg, ClientConnection origin) {

        PlayerCharacter playerCharacter = origin.getPlayerCharacter();

        // Member variable declaration

        RefineMsg msg;

        // Member variable assignment

        msg = (RefineMsg) baseMsg;

        NPC npc = NPC.getFromCache(msg.npcID);

        if (npc == null)
            return true;

        int type = msg.type;
        int token = msg.token;
        boolean worked = false;
        boolean skillPower = true;

        if (type == 0) { //refine skill
            worked = refineSkill(origin, playerCharacter, token, msg);
        } else if (type == 1) { //refine power
            worked = refinePower(origin, playerCharacter, token, msg);
        } else if (type == 2) { //refine stat
            worked = refineStat(origin, playerCharacter, token, msg);
            skillPower = false;
        }

        if (worked) {

            //update player
            playerCharacter.applyBonuses();
            playerCharacter.charItemManager.RemoveEquipmentFromLackOfSkill(playerCharacter, true);

            //echo refine message back

            Dispatch dispatch = Dispatch.borrow(playerCharacter, msg);
            DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);

            //			if (type == 0 && token == 1488335491){
            //				dispatch = Dispatch.borrow(pc, msg);
            //				DispatchMessage.dispatchMsgDispatch(dispatch, engine.Enum.DispatchChannel.SECONDARY);
            //			}

            //resend refine screen

            RefinerScreenMsg refinerScreenMsg = new RefinerScreenMsg(skillPower, npc.getSellPercent(playerCharacter)); //TODO set npc cost
            dispatch = Dispatch.borrow(playerCharacter, refinerScreenMsg);
            DispatchManager.dispatchMsgDispatch(dispatch, mbEnums.DispatchChannel.SECONDARY);
            playerCharacter.recalculate();
        }
        return true;
    }

    public static boolean refineSkill(ClientConnection origin, PlayerCharacter pc, int token, RefineMsg msg) {
        CharacterSkill skill = null;
        ConcurrentHashMap<String, CharacterSkill> skills = pc.getSkills();
        for (CharacterSkill sk : skills.values()) {
            if (sk == null)
                continue;
            SkillsBase sb = sk.getSkillsBase();
            if (sb == null)
                continue;
            if (sb.getToken() == token)
                skill = sk;
        }
        //check if player has skill to refine
        if (skill == null)
            return false;
        //check there's a train to refine
        if (skill.getNumTrains() < 1)
            return false;

        //TODO verify if any skills have this as prereq

        //TODO verify if any powers have this as a prereq
        //get all players powers
        for (CharacterPower power : pc.getPowers().values()) {
            ArrayList<PowerReq> reqs = PowerReq.getPowerReqsForRune(power.getPowerID());
            for (PowerReq req : reqs) {
                ConcurrentHashMap<String, CharacterSkill> playerSkills = pc.getSkills();
                CharacterSkill playerSkill = playerSkills.get(token);
                int currentSkillLevel = playerSkill.getTotalSkillPercet();
                if (token == req.getToken() && req.getLevel() == currentSkillLevel) {
                    return false;
                }
            }
        }

        //refine skill
        return skill.refine(pc);
    }

    public static boolean refinePower(ClientConnection origin, PlayerCharacter pc, int token, RefineMsg msg) {
        CharacterPower power = null;
        ConcurrentHashMap<Integer, CharacterPower> powers = pc.getPowers();
        if (!powers.containsKey(token))
            return false;
        power = powers.get(token);
        if (power == null)
            return false;
        if (power.getTrains() < 1)
            return false;

        //TODO verify if any powers have this as a prereq

        return power.refine(pc);
    }

    public static boolean refineStat(ClientConnection origin, PlayerCharacter pc, int token, RefineMsg msg) {
        if (token == MBServerStatics.STAT_STR_ID)
            return pc.refineStr();
        if (token == MBServerStatics.STAT_DEX_ID)
            return pc.refineDex();
        if (token == MBServerStatics.STAT_CON_ID)
            return pc.refineCon();
        if (token == MBServerStatics.STAT_INT_ID)
            return pc.refineInt(msg);
        if (token == MBServerStatics.STAT_SPI_ID)
            return pc.refineSpi();
        return false;
    }

}