package common.packet;

import java.io.IOException;

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

public class SPacketOpenWindow implements Packet<IClientPlayer>
{
    private int windowId;
    private String inventoryType;
    private String windowTitle;
    private int slotCount;
    private int entityId;
    private BlockPos tilePos;

    public SPacketOpenWindow()
    {
    }

    public SPacketOpenWindow(int incomingWindowId, String incomingWindowTitle, String windowTitleIn)
    {
        this(incomingWindowId, incomingWindowTitle, windowTitleIn, 0);
    }

    public SPacketOpenWindow(int windowIdIn, String guiId, String windowTitleIn, int slotCountIn)
    {
        this.windowId = windowIdIn;
        this.inventoryType = guiId;
        this.windowTitle = windowTitleIn;
        this.slotCount = slotCountIn;
    }

    public SPacketOpenWindow(int windowIdIn, String guiId, String windowTitleIn, int slotCountIn, int incomingEntityId)
    {
        this(windowIdIn, guiId, windowTitleIn, slotCountIn);
        this.entityId = incomingEntityId;
    }

    public SPacketOpenWindow(int windowIdIn, String guiId, String windowTitleIn, int slotCountIn, BlockPos incomingTilePos)
    {
        this(windowIdIn, guiId, windowTitleIn, slotCountIn);
        this.tilePos = incomingTilePos;
    }

    /**
     * Passes this Packet on to the NetHandler for processing.
     */
    public void processPacket(IClientPlayer handler)
    {
        handler.handleOpenWindow(this);
    }

    /**
     * Reads the raw packet data from the data stream.
     */
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.windowId = buf.readUnsignedByte();
        this.inventoryType = buf.readString(32);
        this.windowTitle = buf.readString(256);
        this.slotCount = buf.readUnsignedByte();

        if (this.inventoryType.equals("entity"))
        {
            this.entityId = buf.readInt();
        }
        else if(this.inventoryType.equals("tile")) {
        	this.tilePos = buf.readBlockPos();
        }
    }

    /**
     * Writes the raw packet data to the data stream.
     */
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeByte(this.windowId);
        buf.writeString(this.inventoryType);
        buf.writeString(this.windowTitle);
        buf.writeByte(this.slotCount);

        if (this.inventoryType.equals("entity"))
        {
            buf.writeInt(this.entityId);
        }
        else if(this.inventoryType.equals("tile")) {
        	buf.writeBlockPos(this.tilePos);
        }
    }

    public int getWindowId()
    {
        return this.windowId;
    }

    public String getGuiId()
    {
        return this.inventoryType;
    }

    public String getWindowTitle()
    {
        return this.windowTitle;
    }

    public int getSlotCount()
    {
        return this.slotCount;
    }

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

    public BlockPos getTilePos()
    {
        return this.tilePos;
    }
}
