package engine.net.client.handlers;

import engine.InterestManagement.WorldGrid;
import engine.gameManager.*;
import engine.mbEnums;
import engine.mbEnums.DispatchChannel;
import engine.net.Dispatch;
import engine.net.client.ClientConnection;
import engine.net.client.msg.*;
import engine.objects.*;
import org.pmw.tinylog.Logger;

/*
 * @Author:
 * @Summary: Processes application protocol message which
 * processes training of minions in guard barracks
 */

public class MinionTrainingMsgHandler extends AbstractClientMsgHandler {

    public MinionTrainingMsgHandler() {
        super();
    }

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

        MinionTrainingMessage minionMsg = (MinionTrainingMessage) baseMsg;

        PlayerCharacter playerCharacter = SessionManager.getPlayerCharacter(origin);

        if (playerCharacter == null)
            return true;

        if (minionMsg.getNpcType() == mbEnums.GameObjectType.NPC.ordinal()) {

            NPC npc = NPC.getFromCache(minionMsg.getNpcID());

            if (npc == null)
                return true;

            Building building = BuildingManager.getBuildingFromCache(minionMsg.getBuildingID());

            if (building == null)
                return true;

            //clear minion

            if (npc.minionLock.writeLock().tryLock()) {

                try {
                    if (minionMsg.getType() == 2) {

                        Mob toRemove = Mob.getFromCache(minionMsg.getUUID());

                        if (!npc.minions.contains(toRemove.getObjectUUID()))
                            return true;

                        npc.minions.remove(Integer.valueOf(toRemove.getObjectUUID()));

                        WorldGrid.RemoveWorldObject(toRemove);

                        if (toRemove.getParentZone() != null) {
                            toRemove.getParentZone().zoneMobSet.remove(toRemove);
                        }

                        DbManager.removeFromCache(toRemove);

                        if (toRemove.guardCaptain.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter)) {
                            PlayerCharacter petOwner = (PlayerCharacter) toRemove.guardCaptain;

                            if (petOwner != null) {
                                petOwner.setPet(null);

                                toRemove.guardCaptain = null;
                                PetMsg petMsg = new PetMsg(5, null);
                                Dispatch dispatch = Dispatch.borrow(petOwner, petMsg);
                                DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
                            }
                        }

                        ManageCityAssetsMsg mca1 = new ManageCityAssetsMsg(playerCharacter, building);
                        mca1.actionType = 3;
                        mca1.setTargetType(building.getObjectType().ordinal());
                        mca1.setTargetID(building.getObjectUUID());

                        mca1.setTargetType3(npc.getObjectType().ordinal());
                        mca1.setTargetID3(npc.getObjectUUID());
                        mca1.setAssetName1(building.getName());
                        mca1.setUnknown54(1);

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

                        ManageNPCMsg mnm = new ManageNPCMsg(npc);
                        dispatch = Dispatch.borrow(playerCharacter, mnm);
                        DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);

                        //Add Minion
                    } else {
                        Zone zone = npc.getParentZone();

                        if (zone == null)
                            return true;

                        int maxSlots = 3;

                        if (npc.getContractID() == 842)
                            maxSlots = 1;

                        if (npc.minions.size() == maxSlots)
                            return true;

                        int mobBase;

                        switch (minionMsg.getMinion()) {
                            case 9:
                                mobBase = 13171;
                                break;
                            case 2:
                                mobBase = 13758;
                                break;
                            case 3:
                                mobBase = 13757;
                                break;
                            case 4:
                                mobBase = 2111;
                                break;
                            case 5:
                                mobBase = 12402;
                                break;
                            case 6:
                                mobBase = 2113;
                                break;
                            default:
                                mobBase = minionMsg.getMinion();
                        }

                        if (mobBase == 0)
                            return true;

                        Mob siegeMob = Mob.createSiegeMinion(npc, mobBase);

                        if (siegeMob == null)
                            return true;
                    }

                    ManageNPCMsg mnm = new ManageNPCMsg(npc);
                    mnm.setMessageType(1);
                    Dispatch dispatch = Dispatch.borrow(playerCharacter, mnm);
                    DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);

                } finally {
                    npc.minionLock.writeLock().unlock();
                }
            }

        } else if (minionMsg.getNpcType() == mbEnums.GameObjectType.Mob.ordinal()) {

            Mob npc = Mob.getFromCache(minionMsg.getNpcID());

            if (npc == null)
                return true;

            Building building = BuildingManager.getBuildingFromCache(minionMsg.getBuildingID());

            if (building == null)
                return true;

            //clear minion

            if (npc.minionLock.writeLock().tryLock()) {
                try {
                    if (minionMsg.getType() == 2) {

                        Mob toRemove = Mob.getFromCache(minionMsg.getUUID());

                        if (!npc.minions.contains(toRemove.getObjectUUID()))
                            return true;

                        if (!DbManager.MobQueries.REMOVE_GUARD_MINION(npc.getObjectUUID(), toRemove.firstName))
                            return true;

                        npc.minions.remove(Integer.valueOf(toRemove.getObjectUUID()));

                        WorldGrid.RemoveWorldObject(toRemove);

                        if (toRemove.parentZone != null) {
                            toRemove.parentZone.zoneMobSet.remove(toRemove);
                        }

                        DbManager.removeFromCache(toRemove);

                        if (toRemove.agentType.equals(mbEnums.AIAgentType.SIEGEENGINE)) {

                            PlayerCharacter trebOwner = (PlayerCharacter) toRemove.guardCaptain;

                            if (trebOwner != null) {
                                trebOwner.setPet(null);

                                toRemove.guardCaptain = null;
                                PetMsg petMsg = new PetMsg(5, null);
                                Dispatch dispatch = Dispatch.borrow(trebOwner, petMsg);
                                DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);
                            }
                        }

                        ManageCityAssetsMsg mca1 = new ManageCityAssetsMsg(playerCharacter, building);
                        mca1.actionType = 3;
                        mca1.setTargetType(building.getObjectType().ordinal());
                        mca1.setTargetID(building.getObjectUUID());

                        mca1.setTargetType3(npc.getObjectType().ordinal());
                        mca1.setTargetID3(npc.getObjectUUID());
                        mca1.setAssetName1(building.getName());
                        mca1.setUnknown54(1);

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

                        ManageNPCMsg mnm = new ManageNPCMsg(npc);
                        dispatch = Dispatch.borrow(playerCharacter, mnm);
                        DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);

                        //Add Minion
                    } else {
                        Zone zone = npc.parentZone;

                        if (zone == null)
                            return true;

                        int maxSlots = 5;

                        if (npc.getContract().getContractID() == 842)//artillery captain
                            maxSlots = 1;
                        if (npc.getContract().getContractID() == 910)//guard dogs
                            maxSlots = 0;
                        switch (npc.getRank()) {
                            case 1:
                            case 2:
                                maxSlots = 1;
                                break;
                            case 3:
                                maxSlots = 2;
                                break;
                            case 4:
                            case 5:
                                maxSlots = 3;
                                break;
                            case 6:
                                maxSlots = 4;
                                break;
                            case 7:
                                maxSlots = 5;
                                break;
                        }

                        if (npc.minions.size() == maxSlots)
                            return true;

                        int mobBase = npc.getMobBaseID();

                        if (mobBase == 0)
                            return true;

                        String pirateName = NPCManager.getPirateName(mobBase);

                        Mob toCreate = Mob.createGuardMinion(npc, npc.getLevel(), pirateName);

                        if (toCreate == null)
                            return true;

                        if (!DbManager.MobQueries.ADD_GUARD_MINION(npc.getObjectUUID(), pirateName))
                            return true;

                        if (toCreate != null) {
                            toCreate.setDeathTime(System.currentTimeMillis());
                            toCreate.parentZone.zoneMobSet.add(toCreate);
                        }
                    }

                    ManageNPCMsg mnm = new ManageNPCMsg(npc);
                    mnm.setMessageType(1);
                    Dispatch dispatch = Dispatch.borrow(playerCharacter, mnm);
                    DispatchManager.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);

                } catch (Exception e) {
                    Logger.error(e);
                } finally {

                    npc.minionLock.writeLock().unlock();
                }
            }

        }
        return true;
    }

}