package hmggirlfront.items;

import java.util.List;

import javax.annotation.Nullable;

import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.EntityDoll;
import hmggirlfront.event.level.WorldDifficultyLevel_new;
import hmggirlfront.world.WorldDifficultyLevel;
import net.minecraft.block.BlockLiquid;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.StatList;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.util.text.translation.I18n;
import net.minecraft.world.World;

public class ItemDollCore extends Item
{
	public int ii;
	
	//public String nameid = null;
	
	public int id = 0;
	
	public int craft_time = 200;
	
	public Item main = null;
	public Item off = null;
	
    public ItemDollCore(int i, Item itemmain, Item itemoff)
    {
        this.maxStackSize = 1;
        this.id = i;
        main = itemmain;
        off = itemoff;
    }

    public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
		super.addInformation(stack, worldIn, tooltip, flagIn);
		if(main != null  && main instanceof ItemDollGunBase) {
			ItemDollGunBase gun = (ItemDollGunBase) main;
			tooltip.add(TextFormatting.GREEN + "HP " + ": " + I18n.translateToLocal(String.valueOf(21 + gun.doll_hp)));
			
			tooltip.add(TextFormatting.GREEN + "DEX " + ": " + I18n.translateToLocal(String.valueOf(gun.doll_evasion)));
			tooltip.add(TextFormatting.GREEN + "STR " + ": " + I18n.translateToLocal(String.valueOf(gun.powor)));
			tooltip.add(TextFormatting.GREEN + "ACC " + ": " + I18n.translateToLocal(String.valueOf(gun.bure)));
			tooltip.add(TextFormatting.GREEN + "RPM " + ": " + I18n.translateToLocal(String.valueOf(gun.mobammo)));
			if(gun.doll_armor > 0)
			tooltip.add(TextFormatting.GREEN + "ARMOR " + ": " + I18n.translateToLocal(String.valueOf(gun.doll_armor)));
		}
    }
    
	public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag) {

		{
			//WorldDifficultyLevel.setLevel(mod_GirlFront.INSTANCE, world, 1);
		}
		super.onUpdate(itemstack, world, entity, i, flag);
	}
    
    public void spawnCreature(World worldIn, EntityPlayer playerIn, double par4, double par5, double par6)
    {

    	{
    		++par5;
			int var12 = MathHelper.floor((double) (playerIn.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
			EntityDoll ent = new EntityDoll(worldIn);
			ent.setLocationAndAngles(par4 + 0.5, par5, par6 + 0.5, var12, 0.0F);
			if(main != null) {
				ent.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(main));
			}
			if(off != null) {
				ent.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, new ItemStack(off));
			}
			ent.setcanDespawn(1);
			ent.setsquad(playerIn.getDisplayNameString());
			ent.setTamedBy(playerIn);
			ent.setMoveT(0);
			if(main != null && main instanceof ItemDollGunBase) {
				ItemDollGunBase gun = (ItemDollGunBase) main;
				ent.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(20D + 1 + gun.doll_hp);
				ent.setHealth((float) (20D + 1 + gun.doll_hp));
			}
			
			worldIn.spawnEntity(ent);
			WorldDifficultyLevel_new.setKazu(mod_GirlFront.INSTANCE, worldIn, ++mod_GirlFront.difficult_doll_kazu, "");
			playerIn.addStat(mod_GirlFront.Doll_kazu, 1);
			//int x = playerIn.stat
			if(mod_GirlFront.difficult_level < 1) {
				System.out.println(String.format("update"));
				//playerIn.sendMessage(new TextComponentTranslation("Cancellation of restriction-1", new Object[0]));
				//WorldDifficultyLevel.setLevel(mod_GirlFront.INSTANCE, worldIn, 1, ent, "hmggirlfront.cor.level1.name");
				WorldDifficultyLevel_new.setLevel(mod_GirlFront.INSTANCE, worldIn, 1, "hmggirlfront.cor.level1.name");
				//playerIn.playSound(SoundEvents.UI_TOAST_CHALLENGE_COMPLETE, 1.0F, 1.0F);
			}
			if(mod_GirlFront.difficult_level < 2 && mod_GirlFront.difficult_doll_kazu >= 5) {
				System.out.println(String.format("update"));
				WorldDifficultyLevel_new.setLevel(mod_GirlFront.INSTANCE, worldIn, 2, "hmggirlfront.cor.level2.name");
			}
			
    	}
    	
		/*if (this.id == 1) {
			++par5;
			int var12 = MathHelper.floor((double) (playerIn.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
			EntityDoll ent = new EntityDoll(worldIn);
			ent.setLocationAndAngles(par4 + 0.5, par5, par6 + 0.5, var12, 0.0F);
			ent.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_m1911));
			ent.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, new ItemStack(mod_GirlFront.dollgun_m1911));
			ent.setcanDespawn(1);
			ent.setsquad(playerIn.getDisplayNameString());
			ent.setTamedBy(playerIn);
			ent.setMoveT(0);
			worldIn.spawnEntity(ent);
		} 
		else if (this.id == 2) {
			++par5;
			int var12 = MathHelper.floor((double) (playerIn.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
			EntityDoll ent = new EntityDoll(worldIn);
			ent.setLocationAndAngles(par4 + 0.5, par5, par6 + 0.5, var12, 0.0F);
			ent.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_m1911));
			ent.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, new ItemStack(mod_GirlFront.dollgun_m1911));
			ent.setcanDespawn(1);
			ent.setsquad(playerIn.getDisplayNameString());
			ent.setTamedBy(playerIn);
			ent.setMoveT(0);
			worldIn.spawnEntity(ent);
		}
		else if (this.id == 130) {
			++par5;
			int var12 = MathHelper.floor((double) (playerIn.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
			EntityDoll ent = new EntityDoll(worldIn);
			ent.setLocationAndAngles(par4 + 0.5, par5, par6 + 0.5, var12, 0.0F);
			ent.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_t97));
			ent.setcanDespawn(1);
			ent.setsquad(playerIn.getDisplayNameString());
			ent.setTamedBy(playerIn);
			ent.setMoveT(0);
			worldIn.spawnEntity(ent);
		}
		else if (this.id == 228) {
			++par5;
			int var12 = MathHelper.floor((double) (playerIn.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
			EntityDoll ent = new EntityDoll(worldIn);
			ent.setLocationAndAngles(par4 + 0.5, par5, par6 + 0.5, var12, 0.0F);
			ent.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(mod_GirlFront.dollgun_t100));
			ent.setcanDespawn(1);
			ent.setsquad(playerIn.getDisplayNameString());
			ent.setTamedBy(playerIn);
			ent.setMoveT(0);
			worldIn.spawnEntity(ent);
		}*/
	}
    
    public EnumActionResult onItemUse(EntityPlayer player, World worldIn, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
    {
		ItemStack itemStackIn = player.getHeldItem(hand);
		int par4 = pos.getX();
		int par5 = pos.getY();
		int par6 = pos.getZ();
		
		if (worldIn.isRemote)
        {
			return EnumActionResult.PASS;
        }
        else
		{
        	if (!player.capabilities.isCreativeMode)
            {
        		itemStackIn.shrink(1);
            }
        	spawnCreature(worldIn, player, (double)pos.getX(), (double)pos.getY(), (double)pos.getZ());
        	return EnumActionResult.PASS;
        }
    }
	
	/**
     * Called when the equipped item is right clicked.
     */
    public ActionResult<ItemStack> onItemRightClick(World worldIn, EntityPlayer playerIn, EnumHand handIn)
    {
    	ItemStack itemStackIn = playerIn.getHeldItem(handIn);
        if (worldIn.isRemote)
        {
            return new ActionResult(EnumActionResult.PASS, itemStackIn);
        }
        else
        {
            RayTraceResult raytraceresult = this.rayTrace(worldIn, playerIn, true);

            if (raytraceresult != null && raytraceresult.typeOfHit == RayTraceResult.Type.BLOCK)
            {
                BlockPos blockpos = raytraceresult.getBlockPos();

                if (!(worldIn.getBlockState(blockpos).getBlock() instanceof BlockLiquid))
                {
                    return new ActionResult(EnumActionResult.PASS, itemStackIn);
                }
                else if (worldIn.isBlockModifiable(playerIn, blockpos) && playerIn.canPlayerEdit(blockpos, raytraceresult.sideHit, itemStackIn))
                {
                    spawnCreature(worldIn, playerIn, (double)blockpos.getX(), (double)blockpos.getY(), (double)blockpos.getZ());
                    {

                    	if (!playerIn.capabilities.isCreativeMode)
                        {
                    		itemStackIn.shrink(1);
                        }

                        playerIn.addStat(StatList.getObjectUseStats(this));
                        return new ActionResult(EnumActionResult.SUCCESS, itemStackIn);
                    }
                }
                else
                {
                    return new ActionResult(EnumActionResult.FAIL, itemStackIn);
                }
            }
            else
            {
                return new ActionResult(EnumActionResult.PASS, itemStackIn);
            }
        }
    }
}