package common.inventory;

import common.collect.Lists;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.item.ItemStack;
import common.tags.TagObject;
import java.util.List;
import common.util.BlockPos;

public class InventoryWarpChest extends InventoryBasic
{
    private BlockPos associatedChest;

    public InventoryWarpChest()
    {
        super("Warptruhe", false, 27);
    }

    public void setChestTileEntity(BlockPos chestTileEntity)
    {
        this.associatedChest = chestTileEntity;
    }

    public void readTags(List<TagObject> list)
    {
        for (int i = 0; i < this.getSizeInventory(); ++i)
        {
            this.setInventorySlotContents(i, (ItemStack)null);
        }

        for (int k = 0; k < list.size(); ++k)
        {
            TagObject tag = list.get(k);
            int j = tag.getByte("Slot") & 255;

            if (j >= 0 && j < this.getSizeInventory())
            {
                this.setInventorySlotContents(j, ItemStack.readFromTag(tag));
            }
        }
    }

    public List<TagObject> writeTags()
    {
    	List<TagObject> list = Lists.newArrayList();

        for (int i = 0; i < this.getSizeInventory(); ++i)
        {
            ItemStack itemstack = this.getStackInSlot(i);

            if (itemstack != null)
            {
                TagObject tag = new TagObject();
                tag.setByte("Slot", (byte)i);
                itemstack.writeTags(tag);
                list.add(tag);
            }
        }

        return list;
    }
    
    public boolean isUseableByPlayer(EntityNPC player)
    {
        return (this.associatedChest == null || (player.worldObj.getState(this.associatedChest).getBlock() == Blocks.warp_chest && player.getDistanceSq((double)this.associatedChest.getX() + 0.5D, (double)this.associatedChest.getY() + 0.5D, (double)this.associatedChest.getZ() + 0.5D) <= 64.0D)) && super.isUseableByPlayer(player);
    }

//    public void openInventory(EntityNPC player)
//    {
//        if (this.associatedChest != null)
//        {
//            this.associatedChest.openChest();
//        }
//
//        super.openInventory(player);
//    }

    public void closeInventory(EntityNPC player)
    {
//        if (this.associatedChest != null)
//        {
//            this.associatedChest.closeChest();
//        }

        super.closeInventory(player);
        this.associatedChest = null;
    }
}
