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

package engine.net.client.handlers;

import engine.gameManager.DevCmdManager;
import engine.gameManager.SessionManager;
import engine.mbEnums;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.commands.ClientAdminCommandMsg;
import engine.objects.AbstractGameObject;
import engine.objects.AbstractWorldObject;
import engine.objects.Account;
import engine.objects.PlayerCharacter;
import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger;

public class ClientAdminCommandMsgHandler extends AbstractClientMsgHandler {

    public ClientAdminCommandMsgHandler() {
        super();
    }

    public static boolean processDevCommand(AbstractWorldObject sender, String text) {

        if (sender.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter)) {

            PlayerCharacter pcSender = (PlayerCharacter) sender;

            // first remove the DEV_CMD_PREFIX
            String[] words = text.split(MBServerStatics.DEV_CMD_PREFIX, 2);

            if (words[1].length() == 0)
                return false;

            // next get the command
            String[] commands = words[1].split(" ", 2);
            String cmd = commands[0].toLowerCase();
            String cmdArgument = "";

            if (commands.length > 1)
                cmdArgument = commands[1].trim();

            AbstractGameObject target = pcSender.getLastTarget();
            // return DevCmd.processDevCommand(pcSender, cmd, cmdArgument,
            // target);
            return DevCmdManager.handleDevCmd(pcSender, cmd,
                    cmdArgument, target);
        }
        return false;
    }

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

        PlayerCharacter player = origin.getPlayerCharacter();

        // Member variable declaration

        ClientAdminCommandMsg msg;

        // Member variable assignment

        msg = (ClientAdminCommandMsg) baseMsg;

        if (player == null)
            return true;

        Account acct = SessionManager.getAccount(player);

        if (acct == null)
            return true;

        // require minimal access to continue
        // specific accessLevel checks performed by the DevCmdManager
        if (acct.status.equals(mbEnums.AccountStatus.ADMIN) == false) {
            Logger.warn(player.getFirstName() + " Attempted to use a client admin command");
            //wtf?  ChatManager.chatSystemInfo(pcSender, "CHEATER!!!!!!!!!!!!!");
            return true;
        }

        // First remove the initial slash
        String d = msg.getMsgCommand();
        String[] words = d.split("/", 2);

        if (words[1].length() == 0)
            return true;

        // Next get the command
        String[] commands = words[1].split(" ", 2);
        String cmd = commands[0].toLowerCase();
        String cmdArgument = "";

        if (commands.length > 1)
            cmdArgument = commands[1].trim();

        AbstractGameObject target = msg.getTarget();

        // Map to a DevCmd
        String devCmd = "";

        if (cmd.compareTo("goto") == 0)
            devCmd = "goto";
        else if (cmd.compareTo("suspend") == 0)
            devCmd = "suspend";
        else if (cmd.compareTo("getinfo") == 0)
            devCmd = "info";
        else if (devCmd.isEmpty()) {
            Logger.info("Unhandled admin command was used: /"
                    + cmd);
            return true;
        }

        DevCmdManager.handleDevCmd(player, devCmd, cmdArgument,
                target);
        return true;
    }

}