package common.packet;

import java.io.IOException;
import java.util.List;

import common.entity.DataWatcher;
import common.entity.types.EntityLiving;
import common.init.EntityRegistry;
import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.util.ExtMath;

public class SPacketSpawnMob implements Packet<IClientPlayer>
{
    private int entityId;
    private int type;
    private int x;
    private int y;
    private int z;
    private int velocityX;
    private int velocityY;
    private int velocityZ;
    private byte yaw;
    private byte pitch;
    private byte headPitch;
    private DataWatcher writeWatcher;
    private List<DataWatcher.WatchableObject> watcher;

    public SPacketSpawnMob()
    {
    }

    public SPacketSpawnMob(EntityLiving entityIn)
    {
        this.entityId = entityIn.getId();
        this.type = EntityRegistry.getEntityID(entityIn);
        this.x = ExtMath.floord(entityIn.posX * 32.0D);
        this.y = ExtMath.floord(entityIn.posY * 32.0D);
        this.z = ExtMath.floord(entityIn.posZ * 32.0D);
        this.yaw = (byte)((int)(entityIn.rotYaw * 256.0F / 360.0F));
        this.pitch = (byte)((int)(entityIn.rotPitch * 256.0F / 360.0F));
        this.headPitch = (byte)((int)(entityIn.headYaw * 256.0F / 360.0F));
        double d0 = 3.9D;
        double d1 = entityIn.motionX;
        double d2 = entityIn.motionY;
        double d3 = entityIn.motionZ;

        if (d1 < -d0)
        {
            d1 = -d0;
        }

        if (d2 < -d0)
        {
            d2 = -d0;
        }

        if (d3 < -d0)
        {
            d3 = -d0;
        }

        if (d1 > d0)
        {
            d1 = d0;
        }

        if (d2 > d0)
        {
            d2 = d0;
        }

        if (d3 > d0)
        {
            d3 = d0;
        }

        this.velocityX = (int)(d1 * 8000.0D);
        this.velocityY = (int)(d2 * 8000.0D);
        this.velocityZ = (int)(d3 * 8000.0D);
        this.writeWatcher = entityIn.getDataWatcher();
    }
    
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.entityId = buf.readVarIntFromBuffer();
        this.type = buf.readUnsignedShort();
        this.x = buf.readInt();
        this.y = buf.readInt();
        this.z = buf.readInt();
        this.yaw = buf.readByte();
        this.pitch = buf.readByte();
        this.headPitch = buf.readByte();
        this.velocityX = buf.readShort();
        this.velocityY = buf.readShort();
        this.velocityZ = buf.readShort();
        this.watcher = DataWatcher.readWatchedListFromPacketBuffer(buf);
    }
    
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeVarIntToBuffer(this.entityId);
        buf.writeShort(this.type & 65535);
        buf.writeInt(this.x);
        buf.writeInt(this.y);
        buf.writeInt(this.z);
        buf.writeByte(this.yaw);
        buf.writeByte(this.pitch);
        buf.writeByte(this.headPitch);
        buf.writeShort(this.velocityX);
        buf.writeShort(this.velocityY);
        buf.writeShort(this.velocityZ);
        this.writeWatcher.writeTo(buf);
    }
    
    public void processPacket(IClientPlayer handler)
    {
        handler.handleSpawnMob(this);
    }

    public List<DataWatcher.WatchableObject> getMetadata()
    {
        if (this.watcher == null)
        {
            this.watcher = this.writeWatcher.getAllWatched();
        }

        return this.watcher;
    }

    public int getEntityID()
    {
        return this.entityId;
    }

    public int getEntityType()
    {
        return this.type;
    }

    public int getX()
    {
        return this.x;
    }

    public int getY()
    {
        return this.y;
    }

    public int getZ()
    {
        return this.z;
    }

    public int getVelocityX()
    {
        return this.velocityX;
    }

    public int getVelocityY()
    {
        return this.velocityY;
    }

    public int getVelocityZ()
    {
        return this.velocityZ;
    }

    public byte getYaw()
    {
        return this.yaw;
    }

    public byte getPitch()
    {
        return this.pitch;
    }

    public byte getHeadPitch()
    {
        return this.headPitch;
    }
}
