package electrosphere.net.parser.net.message;

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

public class PlayerMessage extends NetworkMessage {

    public enum PlayerMessageType {
        SET_ID,
        SETINITIALDISCRETEPOSITION,
    }

    PlayerMessageType messageType;
    int playerID;
    int initialDiscretePositionX;
    int initialDiscretePositionY;

    PlayerMessage(PlayerMessageType messageType){
        this.type = MessageType.PLAYER_MESSAGE;
        this.messageType = messageType;
    }

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

    public int getplayerID() {
        return playerID;
    }

    public void setplayerID(int playerID) {
        this.playerID = playerID;
    }

    public int getinitialDiscretePositionX() {
        return initialDiscretePositionX;
    }

    public void setinitialDiscretePositionX(int initialDiscretePositionX) {
        this.initialDiscretePositionX = initialDiscretePositionX;
    }

    public int getinitialDiscretePositionY() {
        return initialDiscretePositionY;
    }

    public void setinitialDiscretePositionY(int initialDiscretePositionY) {
        this.initialDiscretePositionY = initialDiscretePositionY;
    }

    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.PLAYER_MESSAGE_TYPE_SET_ID:
                if(byteStream.size() >= TypeBytes.PLAYER_MESSAGE_TYPE_SET_ID_SIZE){
                    return true;
                } else {
                    return false;
                }
            case TypeBytes.PLAYER_MESSAGE_TYPE_SETINITIALDISCRETEPOSITION:
                if(byteStream.size() >= TypeBytes.PLAYER_MESSAGE_TYPE_SETINITIALDISCRETEPOSITION_SIZE){
                    return true;
                } else {
                    return false;
                }
        }
        return false;
    }

    public static PlayerMessage parseSet_IDMessage(List<Byte> byteStream){
        PlayerMessage rVal = new PlayerMessage(PlayerMessageType.SET_ID);
        stripPacketHeader(byteStream);
        rVal.setplayerID(ByteStreamUtils.popIntFromByteQueue(byteStream));
        return rVal;
    }

    public static PlayerMessage constructSet_IDMessage(int playerID){
        PlayerMessage rVal = new PlayerMessage(PlayerMessageType.SET_ID);
        rVal.setplayerID(playerID);
        rVal.serialize();
        return rVal;
    }

    public static PlayerMessage parseSetInitialDiscretePositionMessage(List<Byte> byteStream){
        PlayerMessage rVal = new PlayerMessage(PlayerMessageType.SETINITIALDISCRETEPOSITION);
        stripPacketHeader(byteStream);
        rVal.setinitialDiscretePositionX(ByteStreamUtils.popIntFromByteQueue(byteStream));
        rVal.setinitialDiscretePositionY(ByteStreamUtils.popIntFromByteQueue(byteStream));
        return rVal;
    }

    public static PlayerMessage constructSetInitialDiscretePositionMessage(int initialDiscretePositionX,int initialDiscretePositionY){
        PlayerMessage rVal = new PlayerMessage(PlayerMessageType.SETINITIALDISCRETEPOSITION);
        rVal.setinitialDiscretePositionX(initialDiscretePositionX);
        rVal.setinitialDiscretePositionY(initialDiscretePositionY);
        rVal.serialize();
        return rVal;
    }

    @Override
    void serialize(){
        byte[] intValues = new byte[8];
        byte[] stringBytes;
        switch(this.messageType){
            case SET_ID:
                rawBytes = new byte[2+4];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_PLAYER;
                //entity messaage header
                rawBytes[1] = TypeBytes.PLAYER_MESSAGE_TYPE_SET_ID;
                intValues = ByteStreamUtils.serializeIntToBytes(playerID);
                for(int i = 0; i < 4; i++){
                    rawBytes[2+i] = intValues[i];
                }
                break;
            case SETINITIALDISCRETEPOSITION:
                rawBytes = new byte[2+4+4];
                //message header
                rawBytes[0] = TypeBytes.MESSAGE_TYPE_PLAYER;
                //entity messaage header
                rawBytes[1] = TypeBytes.PLAYER_MESSAGE_TYPE_SETINITIALDISCRETEPOSITION;
                intValues = ByteStreamUtils.serializeIntToBytes(initialDiscretePositionX);
                for(int i = 0; i < 4; i++){
                    rawBytes[2+i] = intValues[i];
                }
                intValues = ByteStreamUtils.serializeIntToBytes(initialDiscretePositionY);
                for(int i = 0; i < 4; i++){
                    rawBytes[6+i] = intValues[i];
                }
                break;
        }
        serialized = true;
    }

}
