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

package engine.net.client.handlers;

import engine.exception.MsgSendException;
import engine.gameManager.ChatManager;
import engine.net.Protocol;
import engine.net.client.ClientConnection;
import engine.net.client.msg.ClientNetMsg;
import engine.net.client.msg.chat.*;
import engine.objects.PlayerCharacter;

import static engine.gameManager.ChatManager.FLOOD_QTY_THRESHOLD;
import static engine.gameManager.ChatManager.FLOOD_TIME_THRESHOLD;

public class AbstractChatMsgHandler extends AbstractClientMsgHandler {

    public AbstractChatMsgHandler() {
        super(AbstractChatMsg.class);
    }

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

        PlayerCharacter playerCharacter = origin.getPlayerCharacter();

        // Member variable declaration

        AbstractChatMsg msg;

        // Member variable assignment

        msg = (AbstractChatMsg) baseMsg;

        if (msg == null)
            return true;

        if (playerCharacter == null)
            return true;

        Protocol protocolMsg = msg.getProtocolMsg();

        // Flood control, implemented per channel

        boolean isFlood = false;
        long curMsgTime = System.currentTimeMillis();
        long checkTime = playerCharacter.chatFloodTime(protocolMsg.opcode, curMsgTime, FLOOD_QTY_THRESHOLD - 1);

        if ((checkTime > 0L) && (curMsgTime - checkTime < FLOOD_TIME_THRESHOLD))
            isFlood = true;

        switch (protocolMsg) {
            case CHATSAY:
                ChatManager.chatSay(playerCharacter, msg.getMessage(), isFlood);
                return true;
            case CHATCSR:
                ChatManager.chatCSR(msg);
                return true;
            case CHATTELL:
                ChatTellMsg ctm = (ChatTellMsg) msg;
                ChatManager.chatTell(playerCharacter, ctm.getTargetName(), ctm.getMessage(), isFlood);
                return true;
            case CHATSHOUT:
                ChatManager.chatShout(playerCharacter, msg.getMessage(), isFlood);
                return true;
            case CHATGUILD:
                ChatManager.chatGuild(playerCharacter, (ChatGuildMsg) msg);
                return true;
            case CHATGROUP:
                ChatManager.chatGroup(playerCharacter, (ChatGroupMsg) msg);
                return true;
            case CHATIC:
                ChatManager.chatIC(playerCharacter, (ChatICMsg) msg);
                return true;
            case LEADERCHANNELMESSAGE:
            case GLOBALCHANNELMESSAGE:
                ChatManager.chatGlobal(playerCharacter, msg.getMessage(), isFlood);
                return true;
            case CHATPVP:
            case CHATCITY:
            case CHATINFO:
            case SYSTEMBROADCASTCHANNEL:
            default:
        }

        return true;
    }

}