package common.item.tool;

import common.enchantment.Enchantment;
import common.enchantment.EnchantmentHelper;
import common.entity.npc.EntityNPC;
import common.entity.projectile.EntityArrow;
import common.init.Items;
import common.init.SoundEvent;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemAction;
import common.item.ItemStack;
import common.model.Transforms;
import common.world.World;

public class ItemBow extends Item
{
    public ItemBow()
    {
        this.setMaxDamage(384);
        this.setTab(CheatTab.COMBAT);
    }

    /**
     * Called when the player stops using an Item (stops holding the right mouse button).
     */
    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityNPC playerIn, int timeLeft)
    {
        boolean flag = /* playerIn.creative || */ EnchantmentHelper.getEnchantmentLevel(Enchantment.INFINITY, stack) > 0;

        if (flag || playerIn.inventory.hasItem(Items.arrow))
        {
            int i = this.getMaxItemUseDuration(stack) - timeLeft;
            float f = (float)i / 20.0F;
            f = (f * f + f * 2.0F) / 3.0F;

            if ((double)f < 0.1D)
            {
                return;
            }

            if (f > 1.0F)
            {
                f = 1.0F;
            }

            EntityArrow entityarrow = new EntityArrow(worldIn, playerIn, f * 2.0F);

            if (f == 1.0F)
            {
                entityarrow.setIsCritical(true);
            }

            int j = EnchantmentHelper.getEnchantmentLevel(Enchantment.POWER, stack);

            if (j > 0)
            {
                entityarrow.setDamage(entityarrow.getDamage() + (double)j * 0.5D + 0.5D);
            }

            int k = EnchantmentHelper.getEnchantmentLevel(Enchantment.PUNCH, stack);

            if (k > 0)
            {
                entityarrow.setKnockbackStrength(k);
            }

            if (EnchantmentHelper.getEnchantmentLevel(Enchantment.FLAME, stack) > 0)
            {
                entityarrow.setFire(100);
            }

            stack.damageItem(1, playerIn);
            worldIn.playSoundAtEntity(playerIn, SoundEvent.THROW, 1.0F);

            if (flag)
            {
                entityarrow.canBePickedUp = 2;
            }
            else
            {
                playerIn.inventory.consumeInventoryItem(Items.arrow);
            }

//            playerIn.triggerAchievement(StatRegistry.objectUseStats[ItemRegistry.getIdFromItem(this)]);

            if (!worldIn.client)
            {
                worldIn.spawnEntityInWorld(entityarrow);
            }
        }
    }

    /**
     * Called when the player finishes using this Item (E.g. finishes eating.). Not called when the player stops using
     * the Item before the action is complete.
     */
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityNPC playerIn)
    {
        return stack;
    }

    /**
     * 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 ItemAction getItemUseAction(ItemStack stack)
    {
        return ItemAction.AIM;
    }

    /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityNPC playerIn)
    {
        if (/* playerIn.creative || */ playerIn.inventory.hasItem(Items.arrow))
        {
            playerIn.setItemInUse(itemStackIn, this.getMaxItemUseDuration(itemStackIn));
        }

        return itemStackIn;
    }

    /**
     * Return the enchantability factor of the item, most of the time is based on material.
     */
    public int getItemEnchantability()
    {
        return 1;
    }
    
    public String[] getSprites() {
    	return new String[] {"bow_pulling_0", "bow_pulling_1", "bow_pulling_2"};
    }
    
    public String getSprite(EntityNPC player, ItemStack stack) {
    	if(player.getItemInUse() != null) {
            int pull = stack.getMaxItemUseDuration() - player.getItemInUseCount();
            if(pull >= 18)
                return "bow_pulling_2";
            else if(pull > 13)
                return "bow_pulling_1";
            else if(pull > 0)
                return "bow_pulling_0";
        }
    	return null;
    }

    public Transforms getTransform() {
    	return Transforms.RANGED;
    }
    
    public boolean canBeWielded() {
    	return true;
    }
}
