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

package engine.net.client.handlers;

import engine.gameManager.SessionManager;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.login.DeleteCharacterMsg;
import engine.objects.GuildStatusController;
import engine.objects.PlayerCharacter;
import engine.server.MBServerStatics;
import engine.server.login.LoginServer;
import engine.session.Session;
import org.pmw.tinylog.Logger;

public class DeleteCharacterMsgHandler extends AbstractClientMsgHandler {

    public DeleteCharacterMsgHandler() {
        super();
    }

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

        // Member variable declaration

        DeleteCharacterMsg msg;

        // Member variable assignment

        msg = (DeleteCharacterMsg) baseMsg;

        try {
            PlayerCharacter playerCharacter;
            Session session;

            session = SessionManager.getSession(origin);
            playerCharacter = PlayerCharacter.getPlayerCharacter(msg.getCharacterUUID());

            if (playerCharacter == null) {
                Logger.error("Delete Error: PlayerID=" + msg.getCharacterUUID() + " not found.");
                LoginServer.sendCharacterSelectScreen(session);
                return true;
            }

            if (session.getAccount() == null) {
                Logger.error("Delete Error: Account not found.");
                LoginServer.sendCharacterSelectScreen(session);
                return true;
            }

            if (playerCharacter.getAccount().equals(session.getAccount()) == false) {
                Logger.error("Delete Error: Character " + playerCharacter.getName() + " does not belong to account " + origin.getAccount().getUname());
                LoginServer.sendCharacterSelectScreen(session);
                return true;
            }

            //Can't delete as Guild Leader
            //TODO either find an error or just gdisband.

            if (GuildStatusController.isGuildLeader(playerCharacter.getGuildStatus())) {
                LoginServer.KickToLogin(MBServerStatics.LOGINERROR_UNABLE_TO_LOGIN, "Cannot delete a guild leader.", origin);
                return true;
            }

            // check for active banes

            if (LoginServer.getActiveBaneQuery(playerCharacter)) {
                Logger.info("Character " + playerCharacter.getName() + " has unresolved bane");
                LoginServer.KickToLogin(MBServerStatics.LOGINERROR_UNABLE_TO_LOGIN, "Player has unresolved bane.", origin);
                return true;
            }

            playerCharacter.getAccount().characterMap.remove(playerCharacter.getObjectUUID());
            playerCharacter.deactivateCharacter();

            // TODO Delete Equipment
            // Resend Character Select Screen.
            LoginServer.sendCharacterSelectScreen(session);

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

        return true;
    }

}