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


package engine.net.client.handlers;

import engine.exception.MsgSendException;
import engine.gameManager.ChatManager;
import engine.gameManager.DbManager;
import engine.gameManager.GuildManager;
import engine.gameManager.SessionManager;
import engine.mbEnums;
import engine.mbEnums.DispatchChannel;
import engine.mbEnums.GuildHistoryType;
import engine.mbEnums.ItemType;
import engine.mbEnums.OwnerType;
import engine.net.Dispatch;
import engine.net.DispatchMessage;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.ErrorPopupMsg;
import engine.net.client.msg.guild.GuildCreationFinalizeMsg;
import engine.net.client.msg.guild.GuildInfoMsg;
import engine.objects.Guild;
import engine.objects.Item;
import engine.objects.PlayerCharacter;
import engine.util.StringUtils;

public class GuildCreationFinalizeHandler extends AbstractClientMsgHandler {

    public GuildCreationFinalizeHandler() {
        super(GuildCreationFinalizeMsg.class);
    }

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

        PlayerCharacter player;
        GuildCreationFinalizeMsg msg;
        mbEnums.GuildCharterType charterType;
        Guild newGuild;
        Guild playerGuild;

        Item charter;
        Dispatch dispatch;

        msg = (GuildCreationFinalizeMsg) baseMsg;

        player = SessionManager.getPlayerCharacter(origin);
        playerGuild = player.getGuild();

        if (playerGuild.isEmptyGuild() == false && player.getGuild().getGuildLeaderUUID() == player.getObjectUUID()) {
            ErrorPopupMsg.sendErrorPopup(player, GuildManager.MUST_LEAVE_GUILD);
            return true;
        }

        //Validate the Charter

        charter = msg.getCharter();

        if (charter == null || charter.ownerType != OwnerType.PlayerCharacter || charter.getOwnerID() != player.getObjectUUID()) {
            ErrorPopupMsg.sendErrorPopup(player, GuildManager.NO_CHARTER_FOUND);
            return true;
        }

        // Item must be a valid charterType (type 10 in db)

        if (charter == null || (charter.template.item_type.equals(ItemType.CHARTER) == false)) {
            ErrorPopupMsg.sendErrorPopup(player, GuildManager.NO_CHARTER_FOUND);
            return true;
        }

        charterType = mbEnums.GuildCharterType.templateLookup.get(charter.templateID);

        if (charterType == null) {
            ErrorPopupMsg.sendErrorPopup(player, GuildManager.NO_CHARTER_FOUND);
            return true;
        }

        //Validate Guild Tags

        if (!msg.getGuildTag().isValid()) {
            ErrorPopupMsg.sendErrorPopup(player, GuildManager.CREST_RESERVED);
            return true;
        }

        // Validation passes.  Leave current guild and create new one.

        if (player.getGuild() != null && player.getGuild().isEmptyGuild() == false)
            player.getGuild().removePlayer(player, GuildHistoryType.LEAVE);

        int leadershipType = ((msg.getICVoteFlag() << 1) | msg.getMemberVoteFlag());

        newGuild = new Guild(msg.getName(), null, charterType.ordinal(),
                charterType.getLeadershipType(leadershipType), msg.getGuildTag(),
                StringUtils.truncate(msg.getMotto(), 120));

        newGuild.setGuildLeaderForCreate(player);

        synchronized (this) {
            if (!DbManager.GuildQueries.IS_NAME_UNIQUE(msg.getName())) {
                ErrorPopupMsg.sendErrorPopup(player, GuildManager.UNIQUE_NAME);
                return true;
            }

            if (!DbManager.GuildQueries.IS_CREST_UNIQUE(msg.getGuildTag())) {
                ErrorPopupMsg.sendErrorPopup(player, GuildManager.UNIQUE_CREST);
                return true;
            }

            newGuild = DbManager.GuildQueries.SAVE_TO_DATABASE(newGuild);
        }

        if (newGuild == null) {
            ErrorPopupMsg.sendErrorPopup(player, GuildManager.FAILURE_TO_SWEAR_GUILD);
            return true;
        }

        dispatch = Dispatch.borrow(player, msg);
        DispatchMessage.dispatchMsgDispatch(dispatch, DispatchChannel.SECONDARY);

        GuildManager.joinGuild(player, newGuild, GuildHistoryType.CREATE);

        newGuild.setGuildLeader(player);
        player.setGuildLeader(true);
        player.setInnerCouncil(true);
        player.setFullMember(true);
        player.setGuildTitle(charterType.getNumberOfRanks() - 1);
        player.charItemManager.delete(charter);
        player.charItemManager.updateInventory();
        player.incVer();

        DispatchMessage.sendToAllInRange(player, new GuildInfoMsg(player, newGuild, 2));

        ChatManager.chatSystemInfo(player, msg.getName() + " has arrived on Grief server!");

        return true;
    }
}