package hmggirlfront.items;

import java.util.List;

import javax.annotation.Nullable;

import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.EntityDoll;
import hmggirlfront.network.GFMessageKeyPressed;
import hmggirlfront.network.GFPacketHandler;
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.init.Blocks;
import net.minecraft.init.Items;
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.EnumParticleTypes;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;

public class Item_old_dollcore extends Item
{
	public int ii;
	
	//public String nameid = null;
	
	public int id = 0;
	
    public Item_old_dollcore(int i)
    {
        this.maxStackSize = 1;
        this.id = i;
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
	  {
    	tooltip.add(TextFormatting.WHITE + "R-click" + " : " + "Squad Following!");//0
    	tooltip.add(TextFormatting.WHITE + "R-click+Sneaking" + " : " + "Squad Wait!");//3
    	tooltip.add(TextFormatting.WHITE + "L-click" + " : " + "Squad Assault!");//1
    	tooltip.add(TextFormatting.WHITE + "L-click+Sneaking" + " : " + "Squad Assault for LockPoint!");//2
    	tooltip.add(TextFormatting.WHITE + "Gkey-press" + " : " + "Squad Attack for LockPoint!");//5
    	//tooltip.add(TextFormatting.WHITE + "Xkey-press" + " : " + "Open GUI");
    	
    	NBTTagCompound nbt = stack.getTagCompound();
    	if(nbt == null)
			return;
		String id = nbt.getString("name_id");
    	//tooltip.add(TextFormatting.WHITE + "ID " + ":" + I18n.translateToLocal(id));
		//tooltip.add(TextFormatting.WHITE + "ID " + ":" + nameid);
		tooltip.add(TextFormatting.WHITE + "ID_nbt " + ":" + id);
	  }
    
    public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag) {
		
		int s;
		int li = getMaxDamage() - itemstack.getItemDamage();
		
		ItemStack main = new ItemStack(Items.AIR);
		ItemStack off = new ItemStack(Items.AIR);
		ItemStack head = new ItemStack(Items.AIR);
		ItemStack body = new ItemStack(Items.AIR);
		ItemStack leg = new ItemStack(Items.AIR);
		ItemStack feet = new ItemStack(Items.AIR);
		ItemStack ridding = new ItemStack(Items.AIR);
		
		NBTTagCompound nbt = itemstack.getTagCompound();
		if(entity != null && entity instanceof EntityPlayer) {
			EntityPlayer entityplayer = (EntityPlayer) entity;
			if (mod_GirlFront.proxy.keyx() && flag) {
				//GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(1));
			}
			if (mod_GirlFront.proxy.keyg() && flag) {
				GFPacketHandler.INSTANCE.sendToServer(new GFMessageKeyPressed(1011));
				Vec3d lock = Vec3d.fromPitchYaw(entity.rotationPitch, entity.rotationYaw);
				int ix = 0;
				int iy = 0;
				int iz = 0;
				for(int x = 0; x < 60; ++x) {
							if(world.getBlockState(new BlockPos(entity.posX +lock.x * x,  entity.posY + 1.5 +lock.y * x, entity.posZ +lock.z * x)).getBlock()
									!= Blocks.AIR) {
								ix = (int) (entity.posX +lock.x * x);
								iy = (int) (entity.posY + 1.5 +lock.y * x);
								iz = (int) (entity.posZ +lock.z * x);
								{
									double d2 = world.rand.nextGaussian() * 0.02D;
					                double d0 = world.rand.nextGaussian() * 0.02D;
					                double d1 = world.rand.nextGaussian() * 0.02D;
					                entity.world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, 
					                		entity.posX +lock.x * x,  entity.posY + 1.5 +lock.y * x, entity.posZ +lock.z * x, d2, d0, d1);
								}
								break;
							}
				}
			}
			
		}
		
		
		super.onUpdate(itemstack, world, entity, i, flag);
	}
    
    public boolean onEntitySwing(EntityLivingBase entity, ItemStack par1ItemStack) {
		World worldIn = entity.world;
		if (entity != null && entity instanceof EntityPlayer) {
			EntityPlayer playerIn = (EntityPlayer) entity;
			ItemStack itemstack = playerIn.getHeldItemMainhand();
			if (!itemstack.isEmpty()) {
				NBTTagCompound nbt = itemstack.getTagCompound();
	    		String id = nbt.getString("name_id");
				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 pmc = (EntityDoll) entity1;
										if (pmc.isOwner(playerIn)) {
											if(playerIn.isSneaking()) {
												Vec3d lock = Vec3d.fromPitchYawVector(new Vec2f(playerIn.rotationPitch, playerIn.rotationYaw));
												int ix = 0;
												int iy = 0;
												int iz = 0;
												for(int x = 0; x < 60; ++x) {
													//for(int y = 0; y < 60; ++y) 
													{
														//for(int z = 0; z < 60; ++z) 
														{
															if(worldIn.getBlockState(new BlockPos(entity.posX +lock.x * x,  entity.posY + 1.5 +lock.y * x, entity.posZ +lock.z * x)).getBlock()
																	!= Blocks.AIR) {
																ix = (int) (entity.posX +lock.x * x);
																iy = (int) (entity.posY + 1.5 +lock.y * x);
																iz = (int) (entity.posZ +lock.z * x);
																{
																	double d2 = worldIn.rand.nextGaussian() * 0.02D;
													                double d0 = worldIn.rand.nextGaussian() * 0.02D;
													                double d1 = worldIn.rand.nextGaussian() * 0.02D;
													                entity.world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL, 
													                		entity.posX +lock.x * x,  entity.posY + 1.5 +lock.y * x, entity.posZ +lock.z * x, d2, d0, d1);
																}
																break;
															}
														}
													}
												}
												if(ix != 0 && iz != 0 && iy != 0) {
													pmc.setMoveX(ix);
													pmc.setMoveY(iy);
													pmc.setMoveZ(iz);
													pmc.setMoveT(2);
												}
											}else {
												pmc.setMoveT(1);
											}
											break;
										}
									}
								}
							}
						}
					}
				}
			}
		}
    	return false;
    }
    
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
    {
    	ItemStack itemstack = playerIn.getHeldItem(handIn);
    	if(!itemstack.isEmpty()) {
    		NBTTagCompound nbt = itemstack.getTagCompound();
    		String id = nbt.getString("name_id");
    		boolean en = false;
    		{
    			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 pmc = (EntityDoll) entity1;
        								if (pmc.isOwner(playerIn)) 
        								{
        									if(playerIn.isSneaking()) {
        										pmc.setMoveT(3);
        									}else {
        										pmc.setMoveT(0);
        									}
        									en = true;
            								break;
            							}
        	                        }
        						}
        					}
        				}
        			}
        		}
    		}
    		if(!en) {
    			if(!worldIn.isRemote) {
					double x = playerIn.posX;
					double y = playerIn.posY;
					double z = playerIn.posZ;
					 int ix = worldIn.rand.nextInt(9);
				     int iz = worldIn.rand.nextInt(9);
				     EntityDoll ent = new EntityDoll(worldIn);
					ent.setLocationAndAngles(x - 4 + ix, y, z - 4 + iz, 0.0F, 0.0F);
					ent.setcanDespawn(1);
					ent.setnameid(id);
					ent.setsquad(playerIn.getDisplayNameString());
					ent.setTamedBy(playerIn);
					ent.setMoveT(0);
					//if(!main.isEmpty() && main != null && main.getItem() != Items.AIR) 
					{
						//ent.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, main);
					}
					
			        worldIn.spawnEntity(ent);
				}
    		}
    		//else 
    		
    	}
    	/*worldIn.playSound((EntityPlayer)null, 
				playerIn.posX, playerIn.posY, playerIn.posZ, 
				SoundEvents.ENTITY_SNOWBALL_THROW, SoundCategory.NEUTRAL, 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));*/
        playerIn.addStat(StatList.getObjectUseStats(this));
        return new ActionResult(EnumActionResult.SUCCESS, itemstack);
    }
}