package electrosphere.net.parser.net.message;

import io.github.studiorailgun.CircularByteBuffer;
import electrosphere.net.parser.util.ByteStreamUtils;
import java.util.LinkedList;
import java.util.List;

public class CharacterMessage extends NetworkMessage {

    /**
     * The types of messages available in this category.
     */
    public enum CharacterMessageType {
        REQUESTCHARACTERLIST,
        RESPONSECHARACTERLIST,
        REQUESTCREATECHARACTER,
        RESPONSECREATECHARACTERSUCCESS,
        RESPONSECREATECHARACTERFAILURE,
        REQUESTSPAWNCHARACTER,
        RESPONSESPAWNCHARACTER,
        EDITORSWAP,
    }

    /**
     * The type of this message in particular.
     */
    CharacterMessageType messageType;
    String data;

    /**
     * Constructor
     * @param messageType The type of this message
     */
    private CharacterMessage(CharacterMessageType messageType){
        this.type = MessageType.CHARACTER_MESSAGE;
        this.messageType = messageType;
    }

    /**
     * Constructor
     */
    protected CharacterMessage(){
        this.type = MessageType.CHARACTER_MESSAGE;
    }

    public CharacterMessageType getMessageSubtype(){
        return this.messageType;
    }

    /**
     * Gets data
     */
    public String getdata() {
        return data;
    }

    /**
     * Sets data
     */
    public void setdata(String data) {
        this.data = data;
    }

    /**
     * Removes the packet header from the buffer
     * @param byteBuffer The buffer
     */
    static void stripPacketHeader(CircularByteBuffer byteBuffer){
        byteBuffer.read(2);
    }

    /**
     * Checks if this message can be parsed (ie are all bytes present)
     * @param byteBuffer The buffer
     * @param secondByte The second byte, signifying the subtype of the message
     * @return true if the message can be parsed, false otherwise
     */
    public static boolean canParseMessage(CircularByteBuffer byteBuffer, byte secondByte){
        switch(secondByte){
            case TypeBytes.CHARACTER_MESSAGE_TYPE_REQUESTCHARACTERLIST:
                if(byteBuffer.getRemaining() >= TypeBytes.CHARACTER_MESSAGE_TYPE_REQUESTCHARACTERLIST_SIZE){
                    return true;
                } else {
                    return false;
                }
            case TypeBytes.CHARACTER_MESSAGE_TYPE_RESPONSECHARACTERLIST:
                return CharacterMessage.canParseResponseCharacterListMessage(byteBuffer);
            case TypeBytes.CHARACTER_MESSAGE_TYPE_REQUESTCREATECHARACTER:
                return CharacterMessage.canParseRequestCreateCharacterMessage(byteBuffer);
            case TypeBytes.CHARACTER_MESSAGE_TYPE_RESPONSECREATECHARACTERSUCCESS:
                if(byteBuffer.getRemaining() >= TypeBytes.CHARACTER_MESSAGE_TYPE_RESPONSECREATECHARACTERSUCCESS_SIZE){
                    return true;
                } else {
                    return false;
                }
            case TypeBytes.CHARACTER_MESSAGE_TYPE_RESPONSECREATECHARACTERFAILURE:
                if(byteBuffer.getRemaining() >= TypeBytes.CHARACTER_MESSAGE_TYPE_RESPONSECREATECHARACTERFAILURE_SIZE){
                    return true;
                } else {
                    return false;
                }
            case TypeBytes.CHARACTER_MESSAGE_TYPE_REQUESTSPAWNCHARACTER:
                return CharacterMessage.canParseRequestSpawnCharacterMessage(byteBuffer);
            case TypeBytes.CHARACTER_MESSAGE_TYPE_RESPONSESPAWNCHARACTER:
                return CharacterMessage.canParseResponseSpawnCharacterMessage(byteBuffer);
            case TypeBytes.CHARACTER_MESSAGE_TYPE_EDITORSWAP:
                if(byteBuffer.getRemaining() >= TypeBytes.CHARACTER_MESSAGE_TYPE_EDITORSWAP_SIZE){
                    return true;
                } else {
                    return false;
                }
        }
        return false;
    }

    /**
     * Parses a message of type RequestCharacterList
     */
    public static CharacterMessage parseRequestCharacterListMessage(CircularByteBuffer byteBuffer, MessagePool pool){
        CharacterMessage rVal = (CharacterMessage)pool.get(MessageType.CHARACTER_MESSAGE);
        rVal.messageType = CharacterMessageType.REQUESTCHARACTERLIST;
        CharacterMessage.stripPacketHeader(byteBuffer);
        return rVal;
    }

    /**
     * Constructs a message of type RequestCharacterList
     */
    public static CharacterMessage constructRequestCharacterListMessage(){
        CharacterMessage rVal = new CharacterMessage(CharacterMessageType.REQUESTCHARACTERLIST);
        rVal.serialize();
        return rVal;
    }

    /**
     * Checks if a message of type ResponseCharacterList can be parsed from the byte stream
     */
    public static boolean canParseResponseCharacterListMessage(CircularByteBuffer byteBuffer){
        int currentStreamLength = byteBuffer.getRemaining();
        List<Byte> temporaryByteQueue = new LinkedList<Byte>();
        int dataSize = 0;
        if(currentStreamLength < 6){
            return false;
        } else {
            temporaryByteQueue.add(byteBuffer.peek(2 + 0));
            temporaryByteQueue.add(byteBuffer.peek(2 + 1));
            temporaryByteQueue.add(byteBuffer.peek(2 + 2));
            temporaryByteQueue.add(byteBuffer.peek(2 + 3));
            dataSize = ByteStreamUtils.popIntFromByteQueue(temporaryByteQueue);
        }
        if(currentStreamLength < 6 + dataSize){
            return false;
        }
        return true;
    }

    /**
     * Parses a message of type ResponseCharacterList
     */
    public static CharacterMessage parseResponseCharacterListMessage(CircularByteBuffer byteBuffer, MessagePool pool){
        CharacterMessage rVal = (CharacterMessage)pool.get(MessageType.CHARACTER_MESSAGE);
        rVal.messageType = CharacterMessageType.RESPONSECHARACTERLIST;
        CharacterMessage.stripPacketHeader(byteBuffer);
        rVal.setdata(ByteStreamUtils.popStringFromByteQueue(byteBuffer));
        return rVal;
    }

    /**
     * Constructs a message of type ResponseCharacterList
     */
    public static CharacterMessage constructResponseCharacterListMessage(String data){
        CharacterMessage rVal = new CharacterMessage(CharacterMessageType.RESPONSECHARACTERLIST);
        rVal.setdata(data);
        rVal.serialize();
        return rVal;
    }

    /**
     * Checks if a message of type RequestCreateCharacter can be parsed from the byte stream
     */
    public static boolean canParseRequestCreateCharacterMessage(CircularByteBuffer byteBuffer){
        int currentStreamLength = byteBuffer.getRemaining();
        List<Byte> temporaryByteQueue = new LinkedList<Byte>();
        int dataSize = 0;
        if(currentStreamLength < 6){
            return false;
        } else {
            temporaryByteQueue.add(byteBuffer.peek(2 + 0));
            temporaryByteQueue.add(byteBuffer.peek(2 + 1));
            temporaryByteQueue.add(byteBuffer.peek(2 + 2));
            temporaryByteQueue.add(byteBuffer.peek(2 + 3));
            dataSize = ByteStreamUtils.popIntFromByteQueue(temporaryByteQueue);
        }
        if(currentStreamLength < 6 + dataSize){
            return false;
        }
        return true;
    }

    /**
     * Parses a message of type RequestCreateCharacter
     */
    public static CharacterMessage parseRequestCreateCharacterMessage(CircularByteBuffer byteBuffer, MessagePool pool){
        CharacterMessage rVal = (CharacterMessage)pool.get(MessageType.CHARACTER_MESSAGE);
        rVal.messageType = CharacterMessageType.REQUESTCREATECHARACTER;
        CharacterMessage.stripPacketHeader(byteBuffer);
        rVal.setdata(ByteStreamUtils.popStringFromByteQueue(byteBuffer));
        return rVal;
    }

    /**
     * Constructs a message of type RequestCreateCharacter
     */
    public static CharacterMessage constructRequestCreateCharacterMessage(String data){
        CharacterMessage rVal = new CharacterMessage(CharacterMessageType.REQUESTCREATECHARACTER);
        rVal.setdata(data);
        rVal.serialize();
        return rVal;
    }

    /**
     * Parses a message of type ResponseCreateCharacterSuccess
     */
    public static CharacterMessage parseResponseCreateCharacterSuccessMessage(CircularByteBuffer byteBuffer, MessagePool pool){
        CharacterMessage rVal = (CharacterMessage)pool.get(MessageType.CHARACTER_MESSAGE);
        rVal.messageType = CharacterMessageType.RESPONSECREATECHARACTERSUCCESS;
        CharacterMessage.stripPacketHeader(byteBuffer);
        return rVal;
    }

    /**
     * Constructs a message of type ResponseCreateCharacterSuccess
     */
    public static CharacterMessage constructResponseCreateCharacterSuccessMessage(){
        CharacterMessage rVal = new CharacterMessage(CharacterMessageType.RESPONSECREATECHARACTERSUCCESS);
        rVal.serialize();
        return rVal;
    }

    /**
     * Parses a message of type ResponseCreateCharacterFailure
     */
    public static CharacterMessage parseResponseCreateCharacterFailureMessage(CircularByteBuffer byteBuffer, MessagePool pool){
        CharacterMessage rVal = (CharacterMessage)pool.get(MessageType.CHARACTER_MESSAGE);
        rVal.messageType = CharacterMessageType.RESPONSECREATECHARACTERFAILURE;
        CharacterMessage.stripPacketHeader(byteBuffer);
        return rVal;
    }

    /**
     * Constructs a message of type ResponseCreateCharacterFailure
     */
    public static CharacterMessage constructResponseCreateCharacterFailureMessage(){
        CharacterMessage rVal = new CharacterMessage(CharacterMessageType.RESPONSECREATECHARACTERFAILURE);
        rVal.serialize();
        return rVal;
    }

    /**
     * Checks if a message of type RequestSpawnCharacter can be parsed from the byte stream
     */
    public static boolean canParseRequestSpawnCharacterMessage(CircularByteBuffer byteBuffer){
        int currentStreamLength = byteBuffer.getRemaining();
        List<Byte> temporaryByteQueue = new LinkedList<Byte>();
        int dataSize = 0;
        if(currentStreamLength < 6){
            return false;
        } else {
            temporaryByteQueue.add(byteBuffer.peek(2 + 0));
            temporaryByteQueue.add(byteBuffer.peek(2 + 1));
            temporaryByteQueue.add(byteBuffer.peek(2 + 2));
            temporaryByteQueue.add(byteBuffer.peek(2 + 3));
            dataSize = ByteStreamUtils.popIntFromByteQueue(temporaryByteQueue);
        }
        if(currentStreamLength < 6 + dataSize){
            return false;
        }
        return true;
    }

    /**
     * Parses a message of type RequestSpawnCharacter
     */
    public static CharacterMessage parseRequestSpawnCharacterMessage(CircularByteBuffer byteBuffer, MessagePool pool){
        CharacterMessage rVal = (CharacterMessage)pool.get(MessageType.CHARACTER_MESSAGE);
        rVal.messageType = CharacterMessageType.REQUESTSPAWNCHARACTER;
        CharacterMessage.stripPacketHeader(byteBuffer);
        rVal.setdata(ByteStreamUtils.popStringFromByteQueue(byteBuffer));
        return rVal;
    }

    /**
     * Constructs a message of type RequestSpawnCharacter
     */
    public static CharacterMessage constructRequestSpawnCharacterMessage(String data){
        CharacterMessage rVal = new CharacterMessage(CharacterMessageType.REQUESTSPAWNCHARACTER);
        rVal.setdata(data);
        rVal.serialize();
        return rVal;
    }

    /**
     * Checks if a message of type ResponseSpawnCharacter can be parsed from the byte stream
     */
    public static boolean canParseResponseSpawnCharacterMessage(CircularByteBuffer byteBuffer){
        int currentStreamLength = byteBuffer.getRemaining();
        List<Byte> temporaryByteQueue = new LinkedList<Byte>();
        int dataSize = 0;
        if(currentStreamLength < 6){
            return false;
        } else {
            temporaryByteQueue.add(byteBuffer.peek(2 + 0));
            temporaryByteQueue.add(byteBuffer.peek(2 + 1));
            temporaryByteQueue.add(byteBuffer.peek(2 + 2));
            temporaryByteQueue.add(byteBuffer.peek(2 + 3));
            dataSize = ByteStreamUtils.popIntFromByteQueue(temporaryByteQueue);
        }
        if(currentStreamLength < 6 + dataSize){
            return false;
        }
        return true;
    }

    /**
     * Parses a message of type ResponseSpawnCharacter
     */
    public static CharacterMessage parseResponseSpawnCharacterMessage(CircularByteBuffer byteBuffer, MessagePool pool){
        CharacterMessage rVal = (CharacterMessage)pool.get(MessageType.CHARACTER_MESSAGE);
        rVal.messageType = CharacterMessageType.RESPONSESPAWNCHARACTER;
        CharacterMessage.stripPacketHeader(byteBuffer);
        rVal.setdata(ByteStreamUtils.popStringFromByteQueue(byteBuffer));
        return rVal;
    }

    /**
     * Constructs a message of type ResponseSpawnCharacter
     */
    public static CharacterMessage constructResponseSpawnCharacterMessage(String data){
        CharacterMessage rVal = new CharacterMessage(CharacterMessageType.RESPONSESPAWNCHARACTER);
        rVal.setdata(data);
        rVal.serialize();
        return rVal;
    }

    /**
     * Parses a message of type EditorSwap
     */
    public static CharacterMessage parseEditorSwapMessage(CircularByteBuffer byteBuffer, MessagePool pool){
        CharacterMessage rVal = (CharacterMessage)pool.get(MessageType.CHARACTER_MESSAGE);
        rVal.messageType = CharacterMessageType.EDITORSWAP;
        CharacterMessage.stripPacketHeader(byteBuffer);
        return rVal;
    }

    /**
     * Constructs a message of type EditorSwap
     */
    public static CharacterMessage constructEditorSwapMessage(){
        CharacterMessage rVal = new CharacterMessage(CharacterMessageType.EDITORSWAP);
        rVal.serialize();
        return rVal;
    }

    @Override
    void serialize(){
        byte[] intValues = new byte[8];
        byte[] stringBytes;
        switch(this.messageType){
            case REQUESTCHARACTERLIST:
                rawBytes = new byte[2];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_CHARACTER;
                //entity messaage header
                rawBytes[1] = TypeBytes.CHARACTER_MESSAGE_TYPE_REQUESTCHARACTERLIST;
                break;
            case RESPONSECHARACTERLIST:
                rawBytes = new byte[2+4+data.length()];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_CHARACTER;
                //entity messaage header
                rawBytes[1] = TypeBytes.CHARACTER_MESSAGE_TYPE_RESPONSECHARACTERLIST;
                intValues = ByteStreamUtils.serializeIntToBytes(data.length());
                for(int i = 0; i < 4; i++){
                    rawBytes[2+i] = intValues[i];
                }
                stringBytes = data.getBytes();
                for(int i = 0; i < data.length(); i++){
                    rawBytes[6+i] = stringBytes[i];
                }
                break;
            case REQUESTCREATECHARACTER:
                rawBytes = new byte[2+4+data.length()];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_CHARACTER;
                //entity messaage header
                rawBytes[1] = TypeBytes.CHARACTER_MESSAGE_TYPE_REQUESTCREATECHARACTER;
                intValues = ByteStreamUtils.serializeIntToBytes(data.length());
                for(int i = 0; i < 4; i++){
                    rawBytes[2+i] = intValues[i];
                }
                stringBytes = data.getBytes();
                for(int i = 0; i < data.length(); i++){
                    rawBytes[6+i] = stringBytes[i];
                }
                break;
            case RESPONSECREATECHARACTERSUCCESS:
                rawBytes = new byte[2];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_CHARACTER;
                //entity messaage header
                rawBytes[1] = TypeBytes.CHARACTER_MESSAGE_TYPE_RESPONSECREATECHARACTERSUCCESS;
                break;
            case RESPONSECREATECHARACTERFAILURE:
                rawBytes = new byte[2];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_CHARACTER;
                //entity messaage header
                rawBytes[1] = TypeBytes.CHARACTER_MESSAGE_TYPE_RESPONSECREATECHARACTERFAILURE;
                break;
            case REQUESTSPAWNCHARACTER:
                rawBytes = new byte[2+4+data.length()];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_CHARACTER;
                //entity messaage header
                rawBytes[1] = TypeBytes.CHARACTER_MESSAGE_TYPE_REQUESTSPAWNCHARACTER;
                intValues = ByteStreamUtils.serializeIntToBytes(data.length());
                for(int i = 0; i < 4; i++){
                    rawBytes[2+i] = intValues[i];
                }
                stringBytes = data.getBytes();
                for(int i = 0; i < data.length(); i++){
                    rawBytes[6+i] = stringBytes[i];
                }
                break;
            case RESPONSESPAWNCHARACTER:
                rawBytes = new byte[2+4+data.length()];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_CHARACTER;
                //entity messaage header
                rawBytes[1] = TypeBytes.CHARACTER_MESSAGE_TYPE_RESPONSESPAWNCHARACTER;
                intValues = ByteStreamUtils.serializeIntToBytes(data.length());
                for(int i = 0; i < 4; i++){
                    rawBytes[2+i] = intValues[i];
                }
                stringBytes = data.getBytes();
                for(int i = 0; i < data.length(); i++){
                    rawBytes[6+i] = stringBytes[i];
                }
                break;
            case EDITORSWAP:
                rawBytes = new byte[2];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_CHARACTER;
                //entity messaage header
                rawBytes[1] = TypeBytes.CHARACTER_MESSAGE_TYPE_EDITORSWAP;
                break;
        }
        serialized = true;
    }

}
