package common.enchantment;

import common.entity.DamageSource;
import common.entity.Entity;
import common.entity.types.EntityLiving;
import common.init.Config;
import common.item.ItemArmor;
import common.item.ItemStack;
import common.rng.Random;

public class EnchantmentThorns extends Enchantment
{
    public EnchantmentThorns(int p_i45764_1_, String p_i45764_2_, int p_i45764_3_)
    {
        super(p_i45764_1_, p_i45764_2_, p_i45764_3_, EnumEnchantmentType.ARMOR_TORSO);
        this.setName("Dornen");
    }

    /**
     * Returns the minimal value of enchantability needed on the enchantment level passed.
     */
    public int getMinEnchantability(int enchantmentLevel)
    {
        return 10 + 20 * (enchantmentLevel - 1);
    }

    /**
     * Returns the maximum value of enchantability nedded on the enchantment level passed.
     */
    public int getMaxEnchantability(int enchantmentLevel)
    {
        return super.getMinEnchantability(enchantmentLevel) + 50;
    }

    /**
     * Returns the maximum level that the enchantment can have.
     */
    public int getMaxLevel()
    {
        return 3;
    }

    /**
     * Determines if this enchantment can be applied to a specific ItemStack.
     */
    public boolean canApply(ItemStack stack)
    {
        return stack.getItem() instanceof ItemArmor ? true : super.canApply(stack);
    }

    /**
     * Whenever an entity that has this enchantment on one of its associated items is damaged this method will be
     * called.
     */
    public void onUserHurt(EntityLiving user, Entity attacker, int level)
    {
        Random random = user.getRNG();
        ItemStack itemstack = EnchantmentHelper.getEnchantedItem(Enchantment.thorns, user);

        if ((user.worldObj.client || Config.damageThorns) && isHurting(level, random))
        {
            if (attacker != null)
            {
                attacker.attackEntityFrom(DamageSource.causeThornsDamage(user), getDamage(level, random));
//                attacker.playSound("damage.thorns", 0.5F, 1.0F);
            }

            if (itemstack != null)
            {
                itemstack.damageItem(3, user);
            }
        }
        else if (itemstack != null)
        {
            itemstack.damageItem(1, user);
        }
    }

    public static boolean isHurting(int level, Random rand)
    {
        return level <= 0 ? false : rand.floatv() < 0.15F * (float)level;
    }

    public static int getDamage(int level, Random rand)
    {
        return level > 10 ? level - 10 : rand.roll(4);
    }
}
