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 ServerMessage extends NetworkMessage {

    public enum ServerMessageType {
        PING,
        PONG,
    }

    ServerMessageType messageType;

    ServerMessage(ServerMessageType messageType){
        this.type = MessageType.SERVER_MESSAGE;
        this.messageType = messageType;
    }

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

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

    public static boolean canParseMessage(CircularByteBuffer byteBuffer, byte secondByte){
        switch(secondByte){
            case TypeBytes.SERVER_MESSAGE_TYPE_PING:
                if(byteBuffer.getRemaining() >= TypeBytes.SERVER_MESSAGE_TYPE_PING_SIZE){
                    return true;
                } else {
                    return false;
                }
            case TypeBytes.SERVER_MESSAGE_TYPE_PONG:
                if(byteBuffer.getRemaining() >= TypeBytes.SERVER_MESSAGE_TYPE_PONG_SIZE){
                    return true;
                } else {
                    return false;
                }
        }
        return false;
    }

    public static ServerMessage parsePingMessage(CircularByteBuffer byteBuffer){
        ServerMessage rVal = new ServerMessage(ServerMessageType.PING);
        stripPacketHeader(byteBuffer);
        return rVal;
    }

    public static ServerMessage constructPingMessage(){
        ServerMessage rVal = new ServerMessage(ServerMessageType.PING);
        rVal.serialize();
        return rVal;
    }

    public static ServerMessage parsePongMessage(CircularByteBuffer byteBuffer){
        ServerMessage rVal = new ServerMessage(ServerMessageType.PONG);
        stripPacketHeader(byteBuffer);
        return rVal;
    }

    public static ServerMessage constructPongMessage(){
        ServerMessage rVal = new ServerMessage(ServerMessageType.PONG);
        rVal.serialize();
        return rVal;
    }

    @Override
    void serialize(){
        byte[] intValues = new byte[8];
        byte[] stringBytes;
        switch(this.messageType){
            case PING:
                rawBytes = new byte[2];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_SERVER;
                //entity messaage header
                rawBytes[1] = TypeBytes.SERVER_MESSAGE_TYPE_PING;
                break;
            case PONG:
                rawBytes = new byte[2];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_SERVER;
                //entity messaage header
                rawBytes[1] = TypeBytes.SERVER_MESSAGE_TYPE_PONG;
                break;
        }
        serialized = true;
    }

}
