package common.item;

import java.util.List;

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.model.ItemMeshDefinition;
import common.nbt.NBTTagCompound;
import common.nbt.NBTTagList;
import common.rng.Random;

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.enchantmentsBookList) {
            if(enchantment != null && enchantment.type != 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 NBTTagList getEnchantments(ItemStack stack)
    {
        NBTTagCompound nbttagcompound = stack.getTagCompound();
        return nbttagcompound != null && nbttagcompound.hasKey("StoredEnchantments", 9) ? (NBTTagList)nbttagcompound.getTag("StoredEnchantments") : new NBTTagList();
    }

    /**
     * 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);
        NBTTagList nbttaglist = this.getEnchantments(stack);

        if (nbttaglist != null)
        {
            for (int i = 0; i < nbttaglist.tagCount(); ++i)
            {
                int j = nbttaglist.getCompoundTagAt(i).getShort("id");
                int k = nbttaglist.getCompoundTagAt(i).getShort("lvl");

                if (Enchantment.getEnchantmentById(j) != null)
                {
                    tooltip.add(Enchantment.getEnchantmentById(j).getFormattedName(k));
                }
            }
        }
    }

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

        for (int i = 0; i < nbttaglist.tagCount(); ++i)
        {
            NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);

            if (nbttagcompound.getShort("id") == enchantment.enchantmentobj.effectId)
            {
                if (nbttagcompound.getShort("lvl") < enchantment.enchantmentLevel)
                {
                    nbttagcompound.setShort("lvl", (short)enchantment.enchantmentLevel);
                }

                flag = false;
                break;
            }
        }

        if (flag)
        {
            NBTTagCompound nbttagcompound1 = new NBTTagCompound();
            nbttagcompound1.setShort("id", (short)enchantment.enchantmentobj.effectId);
            nbttagcompound1.setShort("lvl", (short)enchantment.enchantmentLevel);
            nbttaglist.appendTag(nbttagcompound1);
        }

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

        stack.getTagCompound().setTag("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, 1, 0);
        EnchantmentHelper.addRandomEnchantment(rand, itemstack, 30);
        return new RngLoot(itemstack, minChance, maxChance, weight);
    }
    
//    public Set<String> getValidTags() {
//    	return Sets.newHashSet("StoredEnchantments");
//    }
//    
//    protected boolean validateNbt(NBTTagCompound tag) {
//    	if(tag.hasKey("StoredEnchantments")) {
//			if(!tag.hasKey("StoredEnchantments", 9)) {
//    			return false;
//    		}
//			NBTTagList ench = tag.getTagList("StoredEnchantments", 10);
//			if(ench.hasNoTags()) {
//    			return false;
//    		}
//			if(ench.tagCount() > Enchantment.getNames().size()) {
//				return false;
//			}
//			Enchantment[] ecn = new Enchantment[ench.tagCount()];
//			for(int e = 0; e < ench.tagCount(); e++) {
//				NBTTagCompound ec = ench.getCompoundTagAt(e);
//				if(ec.getKeySet().size() != 2 || !ec.hasKey("id", 2) || !ec.hasKey("lvl", 2)) {
//					return false;
//				}
//				int id = ec.getShort("id");
//				int lvl = ec.getShort("lvl");
//				Enchantment en = Enchantment.getEnchantmentById(id);
//				if(en == null) {
//					return false;
//				}
////				if(!adv && (lvl < en.getMinLevel() || lvl > en.getMaxLevel())) {
////					return false;
////				}
//				ecn[e] = en;
//			}
//			for(int e = 0; e < ecn.length; e++) {
//				for(int f = 0; f < ecn.length; f++) {
//					if(f != e && ecn[e] == ecn[f]) {
//						return false;
//					}
//				}
//			}
//		}
//    	return true;
//    }
    
    public ItemMeshDefinition getMesher() {
    	return new ItemMeshDefinition()
        {
    		public String getModelLocation(ItemStack stack)
            {
                return "item/enchanted_book#0" + '#' + "inventory";
            }
        };
    }
    
    public void getRenderItems(Item itemIn, List<ItemStack> subItems) {
    	subItems.add(new ItemStack(itemIn, 1, 0));
    }
}
