package hmggirlfront.items;

import java.util.List;

import javax.annotation.Nullable;

import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.EntityDoll;
import hmggirlfront.entity.mob.EntityIBC01;
import hmggirlfront.entity.mob.EntityIBC02;
import hmggirlfront.entity.mob.EntityIBC03;
import hmggirlfront.entity.mob.EntityIBC04;
import hmggirlfront.entity.mob.EntityIBC05;
import hmggirlfront.entity.mob.EntityIBC06;
import hmggirlfront.entity.mob.EntityIBC07;
import hmggirlfront.entity.mob.EntityIBC08;
import hmggirlfront.entity.mob.EntityIBC09;
import hmggirlfront.entity.mob.EntityIBC10;
import hmggirlfront.entity.mob.EntityIBC11;
import hmggirlfront.entity.mob.EntityIBC12;
import hmggirlfront.entity.mob.EntityIBCBase;
import hmggirlfront.entity.mob.EntityIBCBoss01;
import hmggirlfront.entity.mob.IIBCBoss;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.init.SoundEvents;
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.DamageSource;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;

public class ItemMissonCard extends Item
{
	
	public int id = 1;
	
    public ItemMissonCard(int i)
    {
        this.maxStackSize = 1;
        this.id = i;
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
	  {
    	
    	NBTTagCompound nbt = stack.getTagCompound();
    	if(nbt == null)
			return;
    	{
    		int d = nbt.getInteger("destroyed");
    		String d1 = String.format("%1$3d", d);
    		String name ="";
    		String clear ="";
    		if(id == 1) {
    			name = "Destroyed IBC-Machines : ";
    			if(d >= 20) clear =" Clear!";
    		}
    		if(id == 2) {
    			name = "Destroyed IBC-Soldier : ";
    			if(d >= 20) clear =" Clear!";
    		}
    		if(id == 3) {
    			name = "Destroyed IBC-ArmordSoldier : ";
    			if(d >= 10) clear =" Clear!";
    		}
    		if(id == 4) {
    			name = "Invation-Base : ";
    			if(d >= 1) clear =" Clear!";
    		}
    		if(id == 5) {
    			name = "Invation-Headquarters : ";
    			if(d >= 1) clear =" Clear!";
    		}
    		if(id == 6) {
    			name = "Destroyed IBC-BOSS : ";
    			if(d >= 1) clear =" Clear!";
    		}
    		tooltip.add(TextFormatting.WHITE + name + d1 + clear);
    	}
	  }
    
    public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag) {
		
		if(!itemstack.hasTagCompound()) {
			NBTTagCompound nbt = new NBTTagCompound();
			itemstack.setTagCompound(nbt);
			nbt.setInteger("destroyed", 0);
			//System.out.println(String.format("xxx"));
		}
		if(id != 4 && id != 5) {
			if(entity != null) 
			{
				//EntityPlayer entityplayer = (EntityPlayer) entity;
				NBTTagCompound nbt = itemstack.getTagCompound();
				if(nbt != null){
					int d = nbt.getInteger("destroyed");
					double x = entity.posX;
					double y = entity.posY;
					double z = entity.posZ;
					double han = 50;
			    	AxisAlignedBB axisalignedbb2 = (new AxisAlignedBB((double)(x-han), (double)(y-han), (double)(z-han), 
			    			(double)(x + han), (double)(y + han), (double)(z+ han)))
			        		.grow(1);
			        List llist2 = world.getEntitiesWithinAABBExcludingEntity(entity, axisalignedbb2);
			        if(llist2!=null){
			            for (int lj = 0; lj < llist2.size(); lj++) {
			            	
			            	Entity entity1 = (Entity)llist2.get(lj);
			            	if (entity1.canBeCollidedWith() && entity1 != null)
			                {
			            		if(id == 1) {
			            			if (entity1 instanceof EntityIBC01 
			            					|| entity1 instanceof EntityIBC02 
			            					|| entity1 instanceof EntityIBC03
			            					|| entity1 instanceof EntityIBC11)
				                    {
			            				EntityIBCBase en = (EntityIBCBase) entity1;
			            				if(en.getLastDamageSource() != null) {
			            					DamageSource source = en.getLastDamageSource();
			            					Entity attacker = source.getTrueSource();
			            					if(attacker != null && (attacker instanceof EntityDoll || attacker instanceof EntityPlayer)) {
			            						if(en.deathTime == 1) 
							            			{
							            				++d;
							            				nbt.setInteger("destroyed", d);
							            				entity.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
							            			}
			            					}
			            				}
				            			/*if(en.deathTime == 1) 
				            			//if(entity1.isDead)
				            			{
				            				++d;
				            				nbt.setInteger("destroyed", d);
				            				//System.out.println(String.format("%1$3d", nbt.getInteger("destroyed")));
				            				entity.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
				            				//break;
				            			}*/
				                    }
			            		}
			            		if(id == 2) {
			            			if (entity1 instanceof EntityIBC04
			            					|| entity1 instanceof EntityIBC05
			            					|| entity1 instanceof EntityIBC06
			            					|| entity1 instanceof EntityIBC07
			            					|| entity1 instanceof EntityIBC12)
				                    {
			            				EntityIBCBase en = (EntityIBCBase) entity1;
			            				if(en.getLastDamageSource() != null) {
			            					DamageSource source = en.getLastDamageSource();
			            					Entity attacker = source.getTrueSource();
			            					if(attacker != null && (attacker instanceof EntityDoll || attacker instanceof EntityPlayer)) {
			            						if(en.deathTime == 1) 
							            			{
							            				++d;
							            				nbt.setInteger("destroyed", d);
							            				entity.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
							            			}
			            					}
			            				}
				                    }
			            		}
			            		if(id == 3) {
			            			if (entity1 instanceof EntityIBC08
			            					|| entity1 instanceof EntityIBC09
			            					|| entity1 instanceof EntityIBC10)
				                    {
			            				EntityIBCBase en = (EntityIBCBase) entity1;
			            				if(en.getLastDamageSource() != null) {
			            					DamageSource source = en.getLastDamageSource();
			            					Entity attacker = source.getTrueSource();
			            					if(attacker != null && (attacker instanceof EntityDoll || attacker instanceof EntityPlayer)) {
			            						if(en.deathTime == 1) 
							            			{
							            				++d;
							            				nbt.setInteger("destroyed", d);
							            				entity.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
							            			}
			            					}
			            				}
				                    }
			            		}
			            		if(id == 6) {
			            			if (entity1 instanceof IIBCBoss)
				                    {
			            				EntityIBCBase en = (EntityIBCBase) entity1;
			            				if(en.getLastDamageSource() != null) {
			            					DamageSource source = en.getLastDamageSource();
			            					Entity attacker = source.getTrueSource();
			            					if(attacker != null && (attacker instanceof EntityDoll || attacker instanceof EntityPlayer)) {
			            						if(en.deathTime == 1) 
							            			{
							            				++d;
							            				nbt.setInteger("destroyed", d);
							            				entity.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.0F);
							            			}
			            					}
			            				}
				                    }
			            		}
			            		
			                }
			            }
			        }
				}
			}
		}
		
		
		
		super.onUpdate(itemstack, world, entity, i, flag);
	}
    
    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 d = nbt.getInteger("destroyed");
    			if(id == 1) {
    				if(d >= 20) {
        				if(!worldIn.isRemote)
        				{
        					playerIn.dropItem(mod_GirlFront.idoll_bullet, 4);
        					playerIn.dropItem(mod_GirlFront.idoll_parts, 4);
        					playerIn.dropItem(Items.EMERALD, 4);
        					playerIn.dropItem(mod_GirlFront.idoll_doll_contract, 1);
        					playerIn.dropItem(mod_GirlFront.idoll_core, 1);
        				}
        				//if (!playerIn.capabilities.isCreativeMode)
                        {
        					itemstack.shrink(1);
                        }
        			}
    			}
    			if(id == 2) {
    				if(d >= 20) {
        				if(!worldIn.isRemote)
        				{
        					playerIn.dropItem(mod_GirlFront.idoll_bullet, 8);
        					playerIn.dropItem(mod_GirlFront.idoll_parts, 8);
        					playerIn.dropItem(Items.EMERALD, 8);
        					playerIn.dropItem(mod_GirlFront.idoll_doll_contract, 1);
        					playerIn.dropItem(mod_GirlFront.idoll_doll_rapidcontract, 1);
        					playerIn.dropItem(mod_GirlFront.idoll_core, 1);
        				}
        				itemstack.shrink(1);
        			}
    			}
    			if(id == 3) {
    				if(d >= 10) {
        				if(!worldIn.isRemote)
        				{
        					playerIn.dropItem(mod_GirlFront.idoll_bullet, 16);
        					playerIn.dropItem(mod_GirlFront.idoll_parts, 16);
        					playerIn.dropItem(Items.EMERALD, 16);
        					playerIn.dropItem(mod_GirlFront.idoll_doll_contract, 2);
        					playerIn.dropItem(mod_GirlFront.idoll_doll_rapidcontract, 2);
        					playerIn.dropItem(mod_GirlFront.idoll_doll_repair, 2);
        					playerIn.dropItem(mod_GirlFront.idoll_core, 2);
        				}
        				itemstack.shrink(1);
        			}
    			}
    			if(id == 4) {
    				if(d >= 1) {
        				if(!worldIn.isRemote)
        				{
        					playerIn.dropItem(mod_GirlFront.idoll_hp, 16);
        					playerIn.dropItem(mod_GirlFront.idoll_food, 16);
        					playerIn.dropItem(mod_GirlFront.idoll_bullet, 32);
        					playerIn.dropItem(mod_GirlFront.idoll_parts, 16);
        					playerIn.dropItem(Items.EMERALD, 8);
        					playerIn.dropItem(mod_GirlFront.idoll_core, 1);
        				}
        				itemstack.shrink(1);
        			}
    			}
    			if(id == 5) {
    				if(d >= 1) {
        				if(!worldIn.isRemote)
        				{
        					playerIn.dropItem(mod_GirlFront.idoll_hp, 32);
        					playerIn.dropItem(mod_GirlFront.idoll_food, 32);
        					playerIn.dropItem(mod_GirlFront.idoll_bullet, 64);
        					playerIn.dropItem(mod_GirlFront.idoll_parts, 32);
        					playerIn.dropItem(Items.EMERALD, 32);
        					playerIn.dropItem(mod_GirlFront.idoll_core, 6);
        				}
        				itemstack.shrink(1);
        			}
    			}
    			if(id == 6) {
    				if(d >= 1) {
        				if(!worldIn.isRemote)
        				{
        					playerIn.dropItem(Items.EMERALD, 32);
        					playerIn.dropItem(Items.DIAMOND, 4);
        					playerIn.dropItem(mod_GirlFront.idoll_core, 3);
        				}
        				itemstack.shrink(1);
        			}
    			}
    		}
    		
    	}
        playerIn.addStat(StatList.getObjectUseStats(this));
        return new ActionResult(EnumActionResult.SUCCESS, itemstack);
    }
}