package common.packet;

import java.io.IOException;

import common.entity.Entity;
import common.network.IClientPlayer;
import common.network.IPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.world.World;

public class SPacketSkin implements Packet<IClientPlayer>
{
	private int id;
//    private ModelType model;
	private byte[] texture;

    public SPacketSkin()
    {
    }

    public SPacketSkin(int id, byte[] texture) // , ModelType model)
    {
    	this.id = id;
        this.texture = texture;
//    	this.model = model;
    }

    public Entity getEntity(World world) {
        return world.getEntityByID(this.id);
    }
    
    public byte[] getTexture() {
    	return this.texture;
    }
    
//    public ModelType getModel() {
//    	return this.model;
//    }
    
    public void readPacketData(PacketBuffer buf) throws IOException
    {
    	this.id = buf.readVarIntFromBuffer();
//    	this.model = buf.readEnumValue(ModelType.class);
        this.texture = buf.readByteArray();
        if(this.texture.length == 0) {
        	this.texture = null;
        }
        else if(this.texture.length > IPlayer.MAX_SKIN_SIZE) {
        	this.texture = new byte[IPlayer.MAX_SKIN_SIZE];
        }
    }
    
    public void writePacketData(PacketBuffer buf) throws IOException
    {
    	buf.writeVarIntToBuffer(this.id);
//    	buf.writeEnumValue(this.model);
    	buf.writeByteArray(this.texture == null ? new byte[0] : this.texture);
    }
    
    public void processPacket(IClientPlayer handler)
    {
    	handler.handleSkin(this);
    }
}
