package common.enchantment;

import java.util.List;
import java.util.Map;
import java.util.Set;

import common.collect.Lists;
import common.collect.Maps;
import common.entity.DamageSource;
import common.entity.Entity;
import common.entity.types.EntityLiving;
import common.item.ItemStack;

public abstract class Enchantment
{
    private static final String[] LEVELS = new String[] {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"};
    private static final Enchantment[] enchantmentsList = new Enchantment[256];
    public static final Enchantment[] enchantmentsBookList;
    private static final Map<String, Enchantment> locationEnchantments = Maps.<String, Enchantment>newHashMap();
    
    public static final Enchantment protection = new EnchantmentProtection(0, "protection", 10, 0);
    public static final Enchantment fireProtection = new EnchantmentProtection(1, "fire_protection", 5, 1);
    public static final Enchantment featherFalling = new EnchantmentProtection(2, "feather_falling", 5, 2);
    public static final Enchantment blastProtection = new EnchantmentProtection(3, "blast_protection", 2, 3);
    public static final Enchantment projectileProtection = new EnchantmentProtection(4, "projectile_protection", 5, 4);
//    public static final Enchantment respiration = new EnchantmentOxygen(5, "respiration", 2);
//    public static final Enchantment aquaAffinity = new EnchantmentWaterWorker(6, "aqua_affinity", 2);
    public static final Enchantment thorns = new EnchantmentThorns(7, "thorns", 1);
//    public static final Enchantment depthStrider = new EnchantmentWaterWalker(8, "depth_strider", 2);
    public static final Enchantment sharpness = new EnchantmentDamage(16, "sharpness", 10);
//    public static final Enchantment smite = new EnchantmentDamage(17, "smite", 5, 1);
//    public static final Enchantment baneOfArthropods = new EnchantmentDamage(18, "bane_of_arthropods", 5, 2);
    public static final Enchantment knockback = new EnchantmentKnockback(19, "knockback", 5);
    public static final Enchantment fireAspect = new EnchantmentFireAspect(20, "fire_aspect", 2);
    public static final Enchantment looting = new EnchantmentLootBonus(21, "looting", 2, EnumEnchantmentType.WEAPON);
    public static final Enchantment efficiency = new EnchantmentDigging(32, "efficiency", 10);
    public static final Enchantment silkTouch = new EnchantmentUntouching(33, "silk_touch", 1);
    public static final Enchantment unbreaking = new EnchantmentDurability(34, "unbreaking", 5);
    public static final Enchantment fortune = new EnchantmentLootBonus(35, "fortune", 2, EnumEnchantmentType.DIGGER);
    public static final Enchantment power = new EnchantmentArrowDamage(48, "power", 10);
    public static final Enchantment punch = new EnchantmentArrowKnockback(49, "punch", 2);
    public static final Enchantment flame = new EnchantmentArrowFire(50, "flame", 2);
    public static final Enchantment infinity = new EnchantmentArrowInfinite(51, "infinity", 1);
    public static final Enchantment luckOfTheSea = new EnchantmentLootBonus(61, "luck_of_the_sea", 2, EnumEnchantmentType.FISHING_ROD);
    public static final Enchantment lure = new EnchantmentFishingSpeed(62, "lure", 2, EnumEnchantmentType.FISHING_ROD);
    
    public static final Enchantment draining = new EnchantmentDraining(64, "draining", 1);
    
    public final int effectId;
    private final int weight;

    /** The EnumEnchantmentType given to this Enchantment. */
    public EnumEnchantmentType type;

    /** Used in localisation and stats. */
    protected String name;

    /**
     * Retrieves an Enchantment from the enchantmentsList
     */
    public static Enchantment getEnchantmentById(int enchID)
    {
        return enchID >= 0 && enchID < enchantmentsList.length ? enchantmentsList[enchID] : null;
    }

    protected Enchantment(int enchID, String enchName, int enchWeight, EnumEnchantmentType enchType)
    {
        this.effectId = enchID;
        this.weight = enchWeight;
        this.type = enchType;

        if (enchantmentsList[enchID] != null)
        {
            throw new IllegalArgumentException("Duplicate enchantment id!");
        }
        else
        {
            enchantmentsList[enchID] = this;
            locationEnchantments.put(enchName, this);
        }
    }

    /**
     * Retrieves an enchantment by using its location name.
     */
    public static Enchantment getEnchantmentByLocation(String location)
    {
        return locationEnchantments.get(location);
    }

    public static Set<String> getNames()
    {
        return locationEnchantments.keySet();
    }

    /**
     * Retrieves the weight value of an Enchantment. This weight value is used within vanilla to determine how rare an
     * enchantment is.
     */
    public int getWeight()
    {
        return this.weight;
    }

    /**
     * Returns the minimum level that the enchantment can have.
     */
    public int getMinLevel()
    {
        return 1;
    }

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

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

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

    /**
     * Calculates the damage protection of the enchantment based on level and damage source passed.
     */
    public int calcDamageReduction(int level, DamageSource source)
    {
        return 0;
    }

    /**
     * Calculates the additional damage that will be dealt by an item with this enchantment. This alternative to
     * calcModifierDamage is sensitive to the targets EnumCreatureAttribute.
     */
    public int calcAdditionalDamage(int level)
    {
        return 0;
    }

    /**
     * Determines if the enchantment passed can be applyied together with this enchantment.
     */
    public boolean canApplyTogether(Enchantment ench)
    {
        return this != ench;
    }

    /**
     * Sets the enchantment name
     */
    public Enchantment setName(String enchName)
    {
        this.name = enchName;
        return this;
    }

    /**
     * Return the name of key of this enchantment.
     */
    public String getName()
    {
        return this.name;
    }
    
    public String getFormattedName(int level)
    {
        return this.getName() + " " + ((level >= 1 && level <= 10) ? LEVELS[level - 1] : "" + level);
    }

    /**
     * Determines if this enchantment can be applied to a specific ItemStack.
     */
    public boolean canApply(ItemStack stack)
    {
        return this.type.canEnchantItem(stack.getItem());
    }

    /**
     * Called whenever a mob is damaged with an item that has this enchantment on it.
     */
    public void onEntityDamaged(EntityLiving user, Entity target, int level)
    {
    }

    /**
     * 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)
    {
    }

    static
    {
        List<Enchantment> list = Lists.<Enchantment>newArrayList();

        for (Enchantment enchantment : enchantmentsList)
        {
            if (enchantment != null)
            {
                list.add(enchantment);
            }
        }

        enchantmentsBookList = (Enchantment[])list.toArray(new Enchantment[list.size()]);
    }
}
