package hmggirlfront.items;

import java.util.List;

import javax.annotation.Nullable;

import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.npc.EntityNPC_Doll;
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.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.math.BlockPos;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;

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

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn)
	  {
    	TextComponentTranslation information = new TextComponentTranslation("hmggirlfront.mission.inf.right.name", new Object[0]);
		tooltip.add(TextFormatting.WHITE + information.getFormattedText());
    	NBTTagCompound nbt = stack.getTagCompound();
    	if(nbt == null)
			return;
    	{
    		String x = String.format("%1$3d", nbt.getInteger("targetpoint_x"));
    		String y = String.format("%1$3d", nbt.getInteger("targetpoint_y"));
    		String z = String.format("%1$3d", nbt.getInteger("targetpoint_z"));
    		tooltip.add(TextFormatting.WHITE +"TargetPoint_X:"+ x);
    		tooltip.add(TextFormatting.WHITE +"TargetPoint_Y:" + y);
    		tooltip.add(TextFormatting.WHITE +"TargetPoint_Z:" + z);
    	}
    	{
			TextComponentTranslation information2 = new TextComponentTranslation("hmggirlfront.mission1.inf.inf.name", new Object[0]);
			tooltip.add(TextFormatting.WHITE + information2.getFormattedText());
			TextComponentTranslation information3 = new TextComponentTranslation("hmggirlfront.mission1.inf.clear.name", new Object[0]);
			tooltip.add(TextFormatting.GREEN + information3.getFormattedText());
			TextComponentTranslation information4 = new TextComponentTranslation("hmggirlfront.mission1.inf.failed.name", new Object[0]);
			tooltip.add(TextFormatting.RED + information4.getFormattedText());
    	}
    	
	  }
    
    public int time = 0;
    public boolean getnpc = false;
    public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag) {
		super.onUpdate(itemstack, world, entity, i, flag);
		if(!itemstack.isEmpty() && entity != null && entity instanceof EntityPlayer) {
			EntityPlayer player = (EntityPlayer) entity;
			
    		NBTTagCompound nbt = itemstack.getTagCompound();
    		if(nbt != null) {
    			int k = nbt.getInteger("story_progress");
    			if(k < 8) {
    				if(!world.isRemote)++time;
    				if(time > 50) {
    					if(k == 0) player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.gaiyou0.name", new Object[0]));
            			if(k == 1) player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.gaiyou1.name", new Object[0]));
            			if(k == 2) player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.gaiyou2.name", new Object[0]));
            			if(k == 3) player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.gaiyou3.name", new Object[0]));
            			if(k == 4) player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.gaiyou4.name", new Object[0]));
            			if(k == 5)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.gaiyou5.name", new Object[0]));
            			if(k == 6)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.gaiyou6.name", new Object[0]));
            			if(k == 7)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.gaiyou7.name", new Object[0]));
            			++k;
            			nbt.setInteger("story_progress", k);
            			time = 0;
    				}
    			}
    			boolean doll_death = false;
    			List llist = player.world.getEntitiesWithinAABBExcludingEntity(player, player.getEntityBoundingBox()
		        		.expand(player.motionX, player.motionY, player.motionZ).grow(80.0D));
				if (llist != null) {
					for (int lj = 0; lj < llist.size(); lj++) {
						Entity entity1 = (Entity) llist.get(lj);
						if (entity1.canBeCollidedWith() && entity1 != null) {
							if (entity1 instanceof EntityLivingBase && entity1 instanceof EntityNPC_Doll)
	                        {
								EntityNPC_Doll npc_doll = (EntityNPC_Doll) entity1;
								if(npc_doll.getMissonID() == 1) {
									if(npc_doll.task_clear) {
										nbt.setBoolean("task_clear", true);
									}
									if(npc_doll.deathTime > 0) {
										doll_death = true;
									}
									nbt.setBoolean("npc_flag", true);
									//break;
								}
	                        }
						}
					}
				}
				boolean npc_flag = nbt.getBoolean("npc_flag");
				boolean task_clear = nbt.getBoolean("task_clear");
    			if(!getnpc && npc_flag) {
    				getnpc = true;
    				if(!world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.start.name", new Object[0]));
    			}
    			if(npc_flag) {
    				if(task_clear) {
        				if(!world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.clear.name", new Object[0]));
        				if(!world.isRemote)
        				{
        					player.dropItem(mod_GirlFront.idoll_core, 3);
        					player.dropItem(Items.EMERALD, 12);
        					player.dropItem(mod_GirlFront.story_mission_card_2, 1);
        				}
        				itemstack.shrink(1);
        			}else {
        				if(doll_death) {
        					if(!world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.failed.name", new Object[0]));
        					itemstack.shrink(1);
        				}
        				if(player.isDead) {
        					if(!world.isRemote)player.sendMessage(new TextComponentTranslation("hmggirlfront.mission1.failed_player_dead.name", new Object[0]));
        					itemstack.shrink(1);
        				}
        			}
    			}
    			
    		}//nbt
		}
	}
    
    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) {
    			
    		}else {
    			NBTTagCompound newnbt = new NBTTagCompound();
    			
    			if(!worldIn.isRemote && playerIn != null) {
    				double par4 = playerIn.posX + 300;
    				double par6 = playerIn.posZ + 300;
    				BlockPos bp = worldIn.getHeight(new BlockPos(par4, playerIn.posY, par6));
    				int genY = bp.getY(); 
    				double par5 = playerIn.posY; 
    				/*int var12 = MathHelper.floor((double) (playerIn.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
    				EntityNPC_Doll ent = new EntityNPC_Doll(worldIn);
    				ent.setLocationAndAngles(par4 + 0.5, par5, par6 + 0.5, var12, 0.0F);
    				ent.setcanDespawn(1);
    				ent.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_m1911));
    				worldIn.spawnEntity(ent);*/
    				worldIn.setBlockState(new BlockPos(
    						par4, par5, par6), mod_GirlFront.story_mission_1_npc.getDefaultState(), 2);
    				{
    					newnbt.setInteger("story_progress", 0);
    					newnbt.setBoolean("task_clear", false);
    					newnbt.setBoolean("npc_flag", false);
    					newnbt.setInteger("targetpoint_x", (int)par4);
    					newnbt.setInteger("targetpoint_y", (int)par5);
    					newnbt.setInteger("targetpoint_z", (int)par6);
    					String x =  String.format("%1$3d", (int)par4);
    					String y =  String.format("%1$3d", (int)par5);
    					String z =  String.format("%1$3d", (int)par6);
    					playerIn.sendMessage(new TextComponentTranslation(x +"  "+ y +"  "+ z, new Object[0]));
    				}
    			}
    			itemstack.setTagCompound(newnbt);
    		}
    		
    	}
        playerIn.addStat(StatList.getObjectUseStats(this));
        return new ActionResult(EnumActionResult.SUCCESS, itemstack);
    }
}