package common.item.material;

import java.util.List;

import common.collect.Lists;
import common.color.TextColor;
import common.enchantment.Enchantment;
import common.enchantment.EnchantmentHelper;
import common.enchantment.RngEnchantment;
import common.entity.npc.EntityNPC;
import common.init.Items;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.item.RngLoot;
import common.rng.Random;
import common.tags.TagObject;

public class ItemEnchantedBook extends Item
{
    public ItemEnchantedBook() {
        this.setColor(TextColor.YELLOW);
    }
    
    public boolean hasEffect(ItemStack stack)
    {
        return true;
    }

    /**
     * Checks isDamagable and if it cannot be stacked
     */
    public boolean isItemTool(ItemStack stack)
    {
        return false;
    }

    public void getSubItems(Item itemIn, CheatTab tab, List<ItemStack> subItems)
    {
        for (Enchantment enchantment : Enchantment.values()) {
            if(enchantment != null && enchantment.getType() != null)
            	subItems.add(Items.enchanted_book.getEnchantedItemStack(new RngEnchantment(enchantment, enchantment.getMaxLevel())));
        }
    }

//    /**
//     * Return an item rarity from EnumRarity
//     */
//    public ChatFormat getColor(ItemStack stack)
//    {
//        return this.getEnchantments(stack).tagCount() > 0 ? ChatFormat.YELLOW : super.getColor(stack);
//    }

    public List<TagObject> getEnchantments(ItemStack stack)
    {
        TagObject nbttagcompound = stack.getTagCompound();
        return nbttagcompound != null && nbttagcompound.hasList("StoredEnchantments") ? nbttagcompound.getList("StoredEnchantments") : Lists.newArrayList();
    }

    /**
     * allows items to add custom lines of information to the mouseover description
     */
    public void addInformation(ItemStack stack, EntityNPC playerIn, List<String> tooltip)
    {
        super.addInformation(stack, playerIn, tooltip);
        List<TagObject> nbttaglist = this.getEnchantments(stack);

        if (nbttaglist != null)
        {
            for (int i = 0; i < nbttaglist.size(); ++i)
            {
            	Enchantment j = Enchantment.getEnchantment(nbttaglist.get(i).getString("id"));

                if (j != null)
                {
                    int k = nbttaglist.get(i).getShort("lvl");
                    tooltip.add(j.getFormattedName(k));
                }
            }
        }
    }

    /**
     * Adds an stored enchantment to an enchanted book ItemStack
     */
    public void addEnchantment(ItemStack stack, RngEnchantment enchantment)
    {
    	List<TagObject> nbttaglist = this.getEnchantments(stack);
        boolean flag = true;

        for (int i = 0; i < nbttaglist.size(); ++i)
        {
            TagObject nbttagcompound = nbttaglist.get(i);

            if (Enchantment.getEnchantment(nbttagcompound.getString("id")) == enchantment.enchantment)
            {
                if (nbttagcompound.getShort("lvl") < enchantment.level)
                {
                    nbttagcompound.setShort("lvl", (short)enchantment.level);
                }

                flag = false;
                break;
            }
        }

        if (flag)
        {
            TagObject nbttagcompound1 = new TagObject();
            nbttagcompound1.setString("id", enchantment.enchantment.getName());
            nbttagcompound1.setShort("lvl", (short)enchantment.level);
            nbttaglist.add(nbttagcompound1);
        }

        if (!stack.hasTagCompound())
        {
            stack.setTagCompound(new TagObject());
        }

        stack.getTagCompound().setList("StoredEnchantments", nbttaglist);
    }

    /**
     * Returns the ItemStack of an enchanted version of this item.
     */
    public ItemStack getEnchantedItemStack(RngEnchantment data)
    {
        ItemStack itemstack = new ItemStack(this);
        this.addEnchantment(itemstack, data);
        return itemstack;
    }

    public void getAll(Enchantment enchantment, List<ItemStack> list)
    {
        for (int i = enchantment.getMinLevel(); i <= enchantment.getMaxLevel(); ++i)
        {
            list.add(this.getEnchantedItemStack(new RngEnchantment(enchantment, i)));
        }
    }

    public RngLoot getRandom(Random rand)
    {
        return this.getRandom(rand, 1, 1, 1);
    }

    public RngLoot getRandom(Random rand, int minChance, int maxChance, int weight)
    {
        ItemStack itemstack = new ItemStack(Items.book);
        EnchantmentHelper.addRandomEnchantment(rand, itemstack, 30);
        return new RngLoot(itemstack, minChance, maxChance, weight);
    }
}
