package hmggirlfront.gui;

import java.util.List;

import hmggirlfront.block.tile.TileEntityM_Crafting;
import hmggirlfront.block.tile.TileEntityM_Restoring;
import hmggirlfront.block.tile.TileEntity_GKGunRack;
import hmggirlfront.entity.EntityDoll;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.network.IGuiHandler;

public class GuiHandlerGF implements IGuiHandler {
	/*
	 * ServerでGUIが開かれたときに呼ばれる 通常はContainerを生成する。
	 */
	@Override
	public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
		BlockPos pos = new BlockPos(x, y, z);
		if(ID == 0){
        	NBTTagCompound nbt = player.getEntityData();
        	int enid = nbt.getInteger("vi");
        	EntityDoll en = null;
        	List<Entity> llist = player.world.getEntitiesWithinAABBExcludingEntity(player,
        			player.getEntityBoundingBox().expand(player.motionX, player.motionY, player.motionZ).grow(2));
    		if (llist != null) {
    			for (int lj = 0; lj < llist.size(); lj++) {
    				Entity entity1 = (Entity) llist.get(lj);
    				if (entity1.canBeCollidedWith() && entity1.getEntityId() == enid) {
    					en = (EntityDoll) entity1;
    				}
    			}
    		}
    		if(en != null) {
    			return new GFContainerInventory();
    		}else {
    			return null;
    		}
        }
		ItemStack itemstack = player.getHeldItemMainhand();
		if (ID == 1) {
			return new GFContainerInventoryItemSquad(player.inventory, itemstack);
		}
		if (ID == 2) {
			TileEntity tileentity = player.world.getTileEntity(pos);
			if (!world.isBlockLoaded(pos)) {
				return null;
			}else
            if (tileentity instanceof TileEntityM_Crafting)
            {
            	return new GFBlockContainerInventory(player.inventory, (TileEntityM_Crafting)tileentity);
            }
		}
		if (ID == 4) {
			TileEntity tileentity = player.world.getTileEntity(pos);
			if (!world.isBlockLoaded(pos)) {
				return null;
			}else
            if (tileentity instanceof TileEntityM_Restoring)
            {
            	return new GFBlockRestoringContainerInventory(player.inventory, (TileEntityM_Restoring)tileentity);
            }
		}
		
		if(ID == 5){
            return new GFContainer_Pad(player.inventory, itemstack);
        }
		  if (ID == 6) {
				TileEntity tileentity = player.world.getTileEntity(pos);
				if (!world.isBlockLoaded(pos)) {
					return null;
				}else
	            if (tileentity instanceof TileEntity_GKGunRack)
	            {
	            	return new GFBlockContainerInventory_GKGunRack(player.inventory, (TileEntity_GKGunRack)tileentity);
	            }
			}
		return null;
	}

	/*
	 * ClientでGUIが開かれたときに呼ばれる 通常はGUIを生成する
	 */
	@Override
	public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
		BlockPos pos = new BlockPos(x, y, z);

		if(ID == 0){
        	NBTTagCompound nbt = player.getEntityData();
        	int enid = nbt.getInteger("vi");
        	EntityDoll en = null;
        	List<Entity> llist = player.world.getEntitiesWithinAABBExcludingEntity(player,
        			player.getEntityBoundingBox().expand(player.motionX, player.motionY, player.motionZ).grow(2));
    		if (llist != null) {
    			for (int lj = 0; lj < llist.size(); lj++) {
    				Entity entity1 = (Entity) llist.get(lj);
    				if (entity1.canBeCollidedWith() && entity1.getEntityId() == enid) {
    					en = (EntityDoll) entity1;
    				}
    			}
    		}
    		if(en != null) {
    			return new GFGuiInventoryItem(player, en);
    		}else {
    			return null;
    		}
        }
		ItemStack itemstack = player.getHeldItemMainhand();
		if (ID == 1) {
			return new GFGuiInventoryItemSquad(player.inventory, itemstack);
		}
		if (ID == 2) {
			TileEntity tileentity = player.world.getTileEntity(pos);
			if (!world.isBlockLoaded(pos)) {
				return null;
			}else
            if (tileentity instanceof TileEntityM_Crafting)
            {
            	return new GFBlockGuiInventory(player.inventory, (TileEntityM_Crafting)tileentity, (TileEntityM_Crafting)tileentity);
            }
		}
		if (ID == 4) {
			TileEntity tileentity = player.world.getTileEntity(pos);
			if (!world.isBlockLoaded(pos)) {
				return null;
			}else
            if (tileentity instanceof TileEntityM_Restoring)
            {
            	return new GFBlockRestoringGuiInventory(player.inventory, (TileEntityM_Restoring)tileentity, (TileEntityM_Restoring)tileentity);
            }
		}
		
		 if(ID == 5){
	            return new GFGui_Pad(player.inventory, itemstack);
	        }
		 if (ID == 6) {
				TileEntity tileentity = player.world.getTileEntity(pos);
				if (!world.isBlockLoaded(pos)) {
					return null;
				}else
	            if (tileentity instanceof TileEntity_GKGunRack)
	            {
	            	return new GFBlockGuiInventory_GKGunRack(player.inventory, (TileEntity_GKGunRack)tileentity, (TileEntity_GKGunRack)tileentity);
	            }
			}
		return null;
	}
}