package electrosphere.net.parser.net.message;

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

public class StatusMessage extends NetworkMessage {

    public enum StatusMessageType {
        READY,
    }

    StatusMessageType messageType;
    int ready;

    StatusMessage(StatusMessageType messageType){
        this.type = MessageType.STATUS_MESSAGE;
        this.messageType = messageType;
    }

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

    public int getready() {
        return ready;
    }

    public void setready(int ready) {
        this.ready = ready;
    }

    static void stripPacketHeader(List<Byte> byteStream){
        byteStream.remove(0);
        byteStream.remove(0);
    }

    public static boolean canParseMessage(List<Byte> byteStream, byte secondByte){
        switch(secondByte){
            case TypeBytes.STATUS_MESSAGE_TYPE_READY:
                if(byteStream.size() >= TypeBytes.STATUS_MESSAGE_TYPE_READY_SIZE){
                    return true;
                } else {
                    return false;
                }
        }
        return false;
    }

    public static StatusMessage parseReadyMessage(List<Byte> byteStream){
        StatusMessage rVal = new StatusMessage(StatusMessageType.READY);
        stripPacketHeader(byteStream);
        rVal.setready(ByteStreamUtils.popIntFromByteQueue(byteStream));
        return rVal;
    }

    public static StatusMessage constructReadyMessage(int ready){
        StatusMessage rVal = new StatusMessage(StatusMessageType.READY);
        rVal.setready(ready);
        rVal.serialize();
        return rVal;
    }

    @Override
    void serialize(){
        byte[] intValues = new byte[8];
        byte[] stringBytes;
        switch(this.messageType){
            case READY:
                rawBytes = new byte[2+4];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_STATUS;
                //entity messaage header
                rawBytes[1] = TypeBytes.STATUS_MESSAGE_TYPE_READY;
                intValues = ByteStreamUtils.serializeIntToBytes(ready);
                for(int i = 0; i < 4; i++){
                    rawBytes[2+i] = intValues[i];
                }
                break;
        }
        serialized = true;
    }

}
