package electrosphere.net.parser.net.message;

import electrosphere.net.parser.util.ByteStreamUtils;
import electrosphere.net.parser.net.raw.CircularByteBuffer;
import java.util.LinkedList;
import java.util.List;

public class LoreMessage extends NetworkMessage {

    public enum LoreMessageType {
        REQUESTRACES,
        RESPONSERACES,
    }

    LoreMessageType messageType;
    String data;

    LoreMessage(LoreMessageType messageType){
        this.type = MessageType.LORE_MESSAGE;
        this.messageType = messageType;
    }

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

    public String getdata() {
        return data;
    }

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

    static void stripPacketHeader(CircularByteBuffer byteBuffer){
        byteBuffer.read(2);
    }

    public static boolean canParseMessage(CircularByteBuffer byteBuffer, byte secondByte){
        switch(secondByte){
            case TypeBytes.LORE_MESSAGE_TYPE_REQUESTRACES:
                if(byteBuffer.getRemaining() >= TypeBytes.LORE_MESSAGE_TYPE_REQUESTRACES_SIZE){
                    return true;
                } else {
                    return false;
                }
            case TypeBytes.LORE_MESSAGE_TYPE_RESPONSERACES:
                return LoreMessage.canParseResponseRacesMessage(byteBuffer);
        }
        return false;
    }

    public static LoreMessage parseRequestRacesMessage(CircularByteBuffer byteBuffer){
        LoreMessage rVal = new LoreMessage(LoreMessageType.REQUESTRACES);
        stripPacketHeader(byteBuffer);
        return rVal;
    }

    public static LoreMessage constructRequestRacesMessage(){
        LoreMessage rVal = new LoreMessage(LoreMessageType.REQUESTRACES);
        rVal.serialize();
        return rVal;
    }

    public static boolean canParseResponseRacesMessage(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;
    }

    public static LoreMessage parseResponseRacesMessage(CircularByteBuffer byteBuffer){
        LoreMessage rVal = new LoreMessage(LoreMessageType.RESPONSERACES);
        stripPacketHeader(byteBuffer);
        rVal.setdata(ByteStreamUtils.popStringFromByteQueue(byteBuffer));
        return rVal;
    }

    public static LoreMessage constructResponseRacesMessage(String data){
        LoreMessage rVal = new LoreMessage(LoreMessageType.RESPONSERACES);
        rVal.setdata(data);
        rVal.serialize();
        return rVal;
    }

    @Override
    void serialize(){
        byte[] intValues = new byte[8];
        byte[] stringBytes;
        switch(this.messageType){
            case REQUESTRACES:
                rawBytes = new byte[2];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_LORE;
                //entity messaage header
                rawBytes[1] = TypeBytes.LORE_MESSAGE_TYPE_REQUESTRACES;
                break;
            case RESPONSERACES:
                rawBytes = new byte[2+4+data.length()];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_LORE;
                //entity messaage header
                rawBytes[1] = TypeBytes.LORE_MESSAGE_TYPE_RESPONSERACES;
                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;
        }
        serialized = true;
    }

}
