package hmggirlfront.items;

import java.util.List;

import javax.annotation.Nullable;

import hmggirlfront.entity.EntityDoll;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.stats.StatList;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;

public class ItemDollCN extends Item
{
	
    public ItemDollCN()
    {
        this.maxStackSize = 1;
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
	  {
    	tooltip.add(TextFormatting.WHITE + "L-click+Sneaking" + " : " + "Change Squad");//0
    	tooltip.add(TextFormatting.WHITE + "R-click+Sneaking" + " : " + "Gathered Squad");//3
    	tooltip.add(TextFormatting.WHITE + "→←-click" + " : " + "Doll Select");//1
    	tooltip.add(TextFormatting.WHITE + "↑-click" + " : " + "Instruction Doll");//2
    	
    	NBTTagCompound nbt = stack.getTagCompound();
    	if(nbt == null)
			return;
		int squadid = nbt.getInteger("squadid");
		String id = "First";
		if(squadid == 2)id = "Second";
		if(squadid == 3)id = "Third";
		if(squadid == 4)id = "Fourth";
		tooltip.add(TextFormatting.WHITE + id + "Squad");
	  }
    
    public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag) {
		
		if(!itemstack.hasTagCompound()) {
			NBTTagCompound ltags = new NBTTagCompound();
			itemstack.setTagCompound(ltags);
			ltags.setInteger("squadid", 1);
		}
		
		if(entity != null && entity instanceof EntityPlayer) {
			EntityPlayer entityplayer = (EntityPlayer) entity;
			
		}
		
		
		super.onUpdate(itemstack, world, entity, i, flag);
	}
    
    public boolean onEntitySwing(EntityLivingBase entity, ItemStack itemstack) {
    	World worldIn = entity.world;
    	if(entity instanceof EntityPlayer)
    	{
    		EntityPlayer playerIn = (EntityPlayer) entity;
    		if(!itemstack.isEmpty()) {
        		NBTTagCompound nbt = itemstack.getTagCompound();
        		if(nbt != null) {
        			int squadid = nbt.getInteger("squadid");
        			if(playerIn.isSneaking()) {
        				if(squadid < 4) {
            				++squadid;
            			}else {
            				squadid = 1;
            			}
            			nbt.setInteger("squadid", squadid);
            			
            			String id = "First";
            			if(squadid == 2)id = "Second";
            			if(squadid == 3)id = "Third";
            			if(squadid == 4)id = "Fourth";
            			if(worldIn.isRemote)
    	        			playerIn.sendMessage(new TextComponentTranslation(id + "Squad!", new Object[0]));
        			}else {
        				
        			}
        		}
    		}
    	}
    	return false;
    }
    
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
    {
    	ItemStack itemstack = playerIn.getHeldItem(handIn);
    	if(!itemstack.isEmpty()) {
    		NBTTagCompound nbt = itemstack.getTagCompound();
    		if(nbt != null) {
    			int squadid = nbt.getInteger("squadid");
    			if(!playerIn.isSneaking()) {
    				/*if(squadid < 4) {
        				++squadid;
        			}else {
        				squadid = 1;
        			}
        			nbt.setInteger("squadid", squadid);
        			
        			String id = "First";
        			if(squadid == 2)id = "Second";
        			if(squadid == 3)id = "Third";
        			if(squadid == 4)id = "Fourth";
        			if(worldIn.isRemote)
	        			playerIn.sendMessage(new TextComponentTranslation(id + "Squad!", new Object[0]));*/
    				
    				/*int count = 0;
    				{
    					List<Entity> llist = playerIn.world.getEntitiesWithinAABBExcludingEntity(playerIn,
    							playerIn.getEntityBoundingBox().expand(playerIn.motionX, playerIn.motionY, playerIn.motionZ).grow(100.0D));
    					if (llist != null) {
    						for (int lj = 0; lj < llist.size(); lj++) {
    							Entity entity1 = (Entity) llist.get(lj);
    							if (entity1.canBeCollidedWith() && entity1 instanceof EntityDoll && entity1 != null) {
    								EntityDoll doll = (EntityDoll) entity1;
    								if(doll.isOwner(playerIn)  && doll.getSquadID() == squadid) {
    									++count;
    								}
    							}
    						}
    					}
    				}
    				int co = nbt.getInteger("dollcount");
    				{
    					if(co > count) {
    						nbt.setInteger("dollcount", 0);
    					}else {
    						nbt.setInteger("dollcount", co + 1);
    					}
    				}*/
    			}
    			else {
    	    			List<Entity> llist = worldIn.getEntitiesWithinAABBExcludingEntity(playerIn,
    	        				playerIn.getEntityBoundingBox()
    	        						.expand(playerIn.motionX, playerIn.motionY, playerIn.motionZ)
    	        						.grow(50.0D));
    	        		if(playerIn != null){
    	        			if (llist != null) {
    	        				for (int lj = 0; lj < llist.size(); lj++) {
    	        					Entity entity1 = (Entity) llist.get(lj);
    	        					if (entity1.canBeCollidedWith()) {
    	        						if (entity1 != null)
    	        						{
    	        							if (entity1 instanceof EntityLivingBase && entity1 instanceof EntityDoll)
    	        	                        {
    	        								EntityDoll doll = (EntityDoll) entity1;
    	        								if (doll.isOwner(playerIn)  && doll.getSquadID() == squadid) 
    	        								{
    	        									doll.setPositionAndUpdate(playerIn.posX, playerIn.posY, playerIn.posZ);
    	            							}
    	        	                        }
    	        						}
    	        					}
    	        				}
    	        			}
    	        		}
    	        		if(worldIn.isRemote)
    	        			playerIn.sendMessage(new TextComponentTranslation("hmggirlfront.order.comeon.name", new Object[0]));
    			}
    		}
    		
    	}
        playerIn.addStat(StatList.getObjectUseStats(this));
        return new ActionResult(EnumActionResult.SUCCESS, itemstack);
    }
}