package hmggirlfront.items;

import java.util.List;

import javax.annotation.Nullable;

import gvclib.mod_GVCLib;
import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.entity.living.ISoldier;
import gvclib.item.ItemGunBase;
import gvclib.network.GVCLMessageKeyPressed;
import gvclib.network.GVCLPacketHandler;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.EntityDoll;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.monster.IMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.init.MobEffects;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.PotionEffect;
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.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;


	public class ItemSOFLAM extends Item {
		public String text1 = null;
		public String text2 = null;
		public String text3 = null;
		public ItemSOFLAM() {
			super();
			this.maxStackSize = 1;
		}
		
		public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
		  {
	  	if(text1 != null) {
	  		TextComponentTranslation information = new TextComponentTranslation(text1, new Object[0]);
	  		tooltip.add(TextFormatting.WHITE + information.getFormattedText());//0
	  	}
	  	if(text2 != null) {
	  		TextComponentTranslation information = new TextComponentTranslation(text2, new Object[0]);
	  		tooltip.add(TextFormatting.WHITE + information.getFormattedText());//0
	  	}
	  	/*if(text3 != null) {
	  		TextComponentTranslation information = new TextComponentTranslation(text3, new Object[0]);
	  		tooltip.add(TextFormatting.WHITE + information.getFormattedText());//0
	  	}*/
	  	
		  }
		
		public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag)
	    {
			super.onUpdate(itemstack, world, entity, i, flag);
			boolean left = mod_GVCLib.proxy.leftclick();
			if(entity instanceof EntityPlayer && flag && left)
	    	{
				
	    		EntityPlayer playerIn = (EntityPlayer) entity;
	    	}
	    }
		
		public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityLivingBase par3EntityPlayer, int par4){
			
        }
		
		public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
	    {
			ItemStack itemstack = playerIn.getHeldItem(handIn);
			{
				if(playerIn != null){
					
					if (!playerIn.world.isRemote) {
						playerIn.sendMessage(new TextComponentTranslation("hmggirlfront.order.pointfire.name", new Object[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()) {
	        						if (entity1 != null)
	        						{
	        							if (entity1 instanceof EntityLivingBase && entity1 instanceof EntityDoll)
	        	                        {
	        								EntityDoll doll = (EntityDoll) entity1;
	        								boolean hl_flag = false;
	        								if(!doll.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).isEmpty()
	        										&& doll.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).getItem() instanceof ItemDollGunBase) {
	        									ItemDollGunBase gun = (ItemDollGunBase) doll.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).getItem();
	        									if(gun.hoplites_kazu >= 1) {
	        										hl_flag = true;
	        									}
	        								}
	        								if (doll.isOwner(playerIn) && hl_flag) 
	        								{
	        									Vec3d lock = Vec3d.fromPitchYaw(playerIn.rotationPitch, playerIn.rotationYaw);
												int ix = 0;
												int iy = 0;
												int iz = 0;
												for(int x = 0; x < 100; ++x) {
													if (playerIn.world
															.getBlockState(new BlockPos(playerIn.posX + lock.x * x,
																	playerIn.posY + playerIn.eyeHeight + lock.y * x,
																	playerIn.posZ + lock.z * x))
															.getBlock() != Blocks.AIR) {
														ix = (int) (playerIn.posX + lock.x * x);
														iy = (int) (playerIn.posY + playerIn.eyeHeight + lock.y * x);
														iz = (int) (playerIn.posZ + lock.z * x);

														break;
													}
												}
												if(ix != 0 && iz != 0 && iy != 0) {
													doll.setMoveX(ix);
													doll.setMoveY(iy);
													doll.setMoveZ(iz);
													doll.setMoveT(5);
													
													if (!playerIn.world.isRemote) {
													//playerIn.sendMessage(new TextComponentTranslation("hmggirlfront.order.pointfire.name", new Object[0]));
													String mee = doll.getCustomNameTag() + ":" + I18n.format("hmggirlfront.order.roger.name");
													playerIn.sendMessage(new TextComponentTranslation(mee, new Object[0]));
													}
													
													if (!doll.world.isRemote) {
														if(doll.world.getBlockState(new BlockPos(ix, iy + 1, iz)).getBlock() == Blocks.AIR
																|| doll.world.getBlockState(new BlockPos(ix, iy + 1, iz)).getBlock() == mod_GirlFront.bdoll_point){
															doll.world.setBlockState(new BlockPos(ix, iy + 1, iz), mod_GirlFront.bdoll_point.getDefaultState(), 2);
															
														}else {
															boolean flag = false;
															for(int x1 = 0; x1 < 3; ++x1) {
																for(int z1 = 0; z1 < 3; ++z1) {
																	if((doll.world.getBlockState(new BlockPos(ix + x1 - 1, iy + 1, iz + z1 - 1)).getBlock() == Blocks.AIR
																			|| doll.world.getBlockState(new BlockPos(ix + x1 - 1, iy + 1, iz + z1 - 1)).getBlock() == mod_GirlFront.bdoll_point) 
																			&& !flag){
																		doll.world.setBlockState(new BlockPos(ix + x1 - 1, iy + 1, iz + z1 - 1), mod_GirlFront.bdoll_point.getDefaultState(), 2);
																		
																		flag = true;
																		break;
																	}
																}
																if(flag) {
																	break;
																}
															}
														}
													}
												}
	            							}
	        	                        }
	        						}
	        					}
	        				}
	        			}
	        		}
	    		}
			
	    
	            playerIn.setActiveHand(handIn);
	            return new ActionResult(EnumActionResult.SUCCESS, itemstack);
	        }
	    }
		
		  /**
	     * How long it takes to use or consume an item
	     */
	    public int getMaxItemUseDuration(ItemStack stack)
	    {
	        return 72000;
	    }

	    /**
	     * returns the action that specifies what animation to play when the items is being used
	     */
	    public EnumAction getItemUseAction(ItemStack stack)
	    {
	        return EnumAction.BOW;
	    }
}
