package common.packet;

import java.io.IOException;

import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.util.ExtMath;

public class SPacketChangeGameState implements Packet<IClientPlayer>
{
    private Action action;
    private int param;

    public SPacketChangeGameState()
    {
    }

    public SPacketChangeGameState(Action action)
    {
        this(action, 0);
    }

    public SPacketChangeGameState(Action action, float param)
    {
        this(action, (int)(param * 1000.0f));
    }

    public SPacketChangeGameState(Action action, int param)
    {
        this.action = action;
        this.param = param;
    }
    
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.action = buf.readEnumValue(Action.class);
        this.param = buf.readVarInt();
    }
    
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeEnumValue(this.action);
        buf.writeVarInt(this.param);
    }
    
    public void processPacket(IClientPlayer handler)
    {
        handler.handleChangeGameState(this);
    }

    public Action getAction()
    {
        return this.action;
    }

    public int getInt()
    {
        return this.param < 0 ? 0 : this.param;
    }

    public float getFloat(boolean clamp)
    {
        return ExtMath.clampf((float)this.param / 1000.0f, clamp ? 0.0f : -1000000.0f, clamp ? 1.0f : 1000000.0f);
    }
    
    public static enum Action {
    	SET_WEATHER, RAIN_STRENGTH, DARKNESS, FOG_STRENGTH, TEMPERATURE;
    }
}
