package common.item;

import common.enchantment.Enchantment;
import common.enchantment.EnchantmentHelper;
import common.entity.npc.EntityNPC;
import common.entity.projectile.EntityBullet;
import common.init.SoundEvent;
import common.model.Transforms;
import common.world.World;

public abstract class ItemGunBase extends Item
{
	public abstract ItemAmmo getAmmo();
	public abstract float getVelocity();
	
    public ItemGunBase(int durability)
    {
        this.maxStackSize = 1;
        this.setMaxDamage(durability);
        this.setTab(CheatTab.tabCombat);
    }

    public ItemAction getItemPosition(ItemStack stack)
    {
        return ItemAction.AIM;
    }
    
    public ItemStack onItemRightClick(ItemStack stack, World worldIn, EntityNPC playerIn)
    {
        if(stack.getItemDamage() >= this.getMaxDamage())
    		return stack;
    	boolean flag = // playerIn.creative || 
    			EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, stack) > 0;
        if (flag || playerIn.inventory.hasItem(this.getAmmo()))
        {
            EntityBullet bullet = new EntityBullet(worldIn, playerIn, this.getVelocity());
            bullet.setDamage(this.getAmmo().getDamage(stack));

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

            if (j > 0)
            {
                bullet.setDamage(bullet.getDamage() + j);
            }

//            int k = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, stack);
//
//            if (k > 0)
//            {
//                entityarrow.setKnockbackStrength(k);
//            }

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

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

            if(!flag)
                playerIn.inventory.consumeInventoryItem(this.getAmmo());

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

            if (!worldIn.client)
            {
                worldIn.spawnEntityInWorld(bullet);
            }
        }
        return stack;
    }
    
    public int getItemEnchantability()
    {
        return 1;
    }

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