package common.init;

import java.util.Map;
import java.util.Map.Entry;

import common.collect.Maps;
import common.item.Item;
import common.item.ItemStack;
import common.item.tool.ItemFishFood;

import java.util.Set;

public abstract class SmeltingRegistry
{
    private static final Map<ItemStack, ItemStack> smeltingList = Maps.<ItemStack, ItemStack>newHashMap();
    private static final Map<ItemStack, Float> experienceList = Maps.<ItemStack, Float>newHashMap();
    
    private static boolean compareItemStacks(ItemStack stack1, ItemStack stack2)
    {
        return stack2.getItem() == stack1.getItem();
    }

    static void register()
    {
        add(Items.sand, new ItemStack(Items.glass), 0.1F);
        add(Items.red_sand, new ItemStack(Items.glass), 0.1F);
        add(Items.porkchop, new ItemStack(Items.cooked_porkchop), 0.35F);
        add(Items.beef, new ItemStack(Items.cooked_beef), 0.35F);
        add(Items.chicken, new ItemStack(Items.cooked_chicken), 0.35F);
        add(Items.cobblestone, new ItemStack(Items.stone), 0.1F);
        add(Items.stonebrick, new ItemStack(Items.cracked_stonebrick), 0.1F);
        add(Items.clay_ball, new ItemStack(Items.brick), 0.3F);
        add(Items.clay, new ItemStack(Items.hardened_clay), 0.35F);
        add(Items.cactus, new ItemStack(Items.cactus_green), 0.2F);
        add(Items.potato, new ItemStack(Items.baked_potato), 0.35F);
        add(Items.hellrock, new ItemStack(Items.bloodbrick), 0.1F);

        for (ItemFishFood.FishType fish : ItemFishFood.FishType.values())
        {
            if (fish.canCook())
            {
                add(ItemRegistry.getRegisteredItem(fish.getName()), new ItemStack(ItemRegistry.getRegisteredItem("cooked_" + fish.getName())), 0.35F);
            }
        }

        add(Items.emerald_ore, new ItemStack(Items.emerald), 1.0F);
        add(Items.coal_ore, new ItemStack(Items.coal), 0.1F);
        add(Items.lapis_ore, new ItemStack(Items.lapis_lazuli), 0.2F);
        add(Items.quartz_ore, new ItemStack(Items.quartz), 0.2F);
        add(Items.redstone_ore, new ItemStack(Items.redstone), 0.7F);

        for(OreType ore : OreType.values()) {
        	Item item = ItemRegistry.getRegisteredItem(ore.item);
            add(ItemRegistry.getRegisteredItem(ore.name + "_ore"), new ItemStack(item), ((float)ore.experience) / 3.0F);
        }
        for(MetalType metal : MetalType.values()) {
        	Item item = ItemRegistry.getRegisteredItem(metal.isPowder ? (metal.name + "_powder") : (metal.name + "_ingot"));
            add(ItemRegistry.getRegisteredItem(metal.name + "_ore"), new ItemStack(item), 0.7F);
        }
        for(WoodType wood : WoodType.values()) {
            add(ItemRegistry.getRegisteredItem(wood.getName() + "_log"), new ItemStack(Items.charcoal), 0.15F);
        }
    }
    
    private static void add(Item input, ItemStack stack, float experience)
    {
        add(new ItemStack(input), stack, experience);
    }
    
    private static void add(ItemStack input, ItemStack stack, float experience)
    {
        smeltingList.put(input, stack);
        experienceList.put(stack, Float.valueOf(experience));
    }
    
    public static ItemStack getResult(ItemStack stack)
    {
        for (Entry<ItemStack, ItemStack> entry : smeltingList.entrySet())
        {
            if (compareItemStacks(stack, (ItemStack)entry.getKey()))
            {
                return (ItemStack)entry.getValue();
            }
        }

        return null;
    }

    public static float getExperience(ItemStack stack)
    {
        for (Entry<ItemStack, Float> entry : experienceList.entrySet())
        {
            if (compareItemStacks(stack, (ItemStack)entry.getKey()))
            {
                return ((Float)entry.getValue()).floatValue();
            }
        }

        return 0.0F;
    }

    public static void getSmeltingList(Set<Item> set)
    {
    	for (ItemStack itemstack : smeltingList.values())
        {
            set.add(itemstack.getItem());
        }
    }
}
