package electrosphere.net.parser.net.message;

import io.github.studiorailgun.CircularByteBuffer;
public class ServerMessage extends NetworkMessage {

    /**
     * The types of messages available in this category.
     */
    public enum ServerMessageType {
        PING,
        PONG,
    }

    /**
     * The type of this message in particular.
     */
    ServerMessageType messageType;

    /**
     * Constructor
     * @param messageType The type of this message
     */
    private ServerMessage(ServerMessageType messageType){
        this.type = MessageType.SERVER_MESSAGE;
        this.messageType = messageType;
    }

    /**
     * Constructor
     */
    protected ServerMessage(){
        this.type = MessageType.SERVER_MESSAGE;
    }

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

    /**
     * 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.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;
    }

    /**
     * Parses a message of type Ping
     */
    public static ServerMessage parsePingMessage(CircularByteBuffer byteBuffer, MessagePool pool){
        ServerMessage rVal = (ServerMessage)pool.get(MessageType.SERVER_MESSAGE);
        rVal.messageType = ServerMessageType.PING;
        ServerMessage.stripPacketHeader(byteBuffer);
        return rVal;
    }

    /**
     * Constructs a message of type Ping
     */
    public static ServerMessage constructPingMessage(){
        ServerMessage rVal = new ServerMessage(ServerMessageType.PING);
        rVal.serialize();
        return rVal;
    }

    /**
     * Parses a message of type Pong
     */
    public static ServerMessage parsePongMessage(CircularByteBuffer byteBuffer, MessagePool pool){
        ServerMessage rVal = (ServerMessage)pool.get(MessageType.SERVER_MESSAGE);
        rVal.messageType = ServerMessageType.PONG;
        ServerMessage.stripPacketHeader(byteBuffer);
        return rVal;
    }

    /**
     * Constructs a message of type Pong
     */
    public static ServerMessage constructPongMessage(){
        ServerMessage rVal = new ServerMessage(ServerMessageType.PONG);
        rVal.serialize();
        return rVal;
    }

    @Override
    void serialize(){
        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;
    }

}
