package common.init;

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

import common.block.Block;
import common.block.BlockStoneBrick;
import common.collect.Maps;
import common.color.DyeColor;
import common.item.Item;
import common.item.ItemFishFood;
import common.item.ItemStack;

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() && (stack2.getMetadata() == 32767 || stack2.getMetadata() == stack1.getMetadata());
    }

    static void register()
    {
//        add(Blocks.iron_ore, new ItemStack(Items.iron_ingot), 0.7F);
//        add(Blocks.gold_ore, new ItemStack(Items.gold_ingot), 1.0F);
//        add(Blocks.diamond_ore, new ItemStack(Items.diamond), 1.0F);
        add(Blocks.sand, new ItemStack(Blocks.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);
//        addSmelting(Items.rabbit, new ItemStack(Items.cooked_rabbit), 0.35F);
//        addSmelting(Items.mutton, new ItemStack(Items.cooked_mutton), 0.35F);
        add(Blocks.cobblestone, new ItemStack(Blocks.stone), 0.1F);
        add(new ItemStack(Blocks.stonebrick, 1, BlockStoneBrick.DEFAULT_META), new ItemStack(Blocks.stonebrick, 1, BlockStoneBrick.CRACKED_META), 0.1F);
        add(Items.clay_ball, new ItemStack(Items.brick), 0.3F);
        add(Blocks.clay, new ItemStack(Blocks.hardened_clay), 0.35F);
        add(Blocks.cactus, new ItemStack(Items.dye, 1, DyeColor.GREEN.getDyeDamage()), 0.2F);
        add(Items.potato, new ItemStack(Items.baked_potato), 0.35F);
        add(Blocks.hellrock, new ItemStack(Items.bloodbrick), 0.1F);
//        add(new ItemStack(Blocks.sponge, 1, 1), new ItemStack(Blocks.sponge, 1, 0), 0.15F);

        for (ItemFishFood.FishType itemfishfood$fishtype : ItemFishFood.FishType.values())
        {
            if (itemfishfood$fishtype.canCook())
            {
                add(new ItemStack(Items.fish, 1, itemfishfood$fishtype.getMetadata()), new ItemStack(Items.cooked_fish, 1, itemfishfood$fishtype.getMetadata()), 0.35F);
            }
        }

        add(Blocks.emerald_ore, new ItemStack(Items.emerald), 1.0F);
        add(Blocks.coal_ore, new ItemStack(Items.coal), 0.1F);
        add(Blocks.lapis_ore, new ItemStack(Items.dye, 1, DyeColor.BLUE.getDyeDamage()), 0.2F);
        add(Blocks.quartz_ore, new ItemStack(Items.quartz), 0.2F);
        add(Blocks.redstone_ore, new ItemStack(Items.redstone), 0.7F);

        for(OreType ore : OreType.values()) {
        	Item item = ItemRegistry.getRegisteredItem(ore.item);
            add(BlockRegistry.getRegisteredBlock(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(BlockRegistry.getRegisteredBlock(metal.name + "_ore"), new ItemStack(item), 0.7F);
        }
        for(WoodType wood : WoodType.values()) {
            add(BlockRegistry.getRegisteredBlock(wood.getName() + "_log"), new ItemStack(Items.coal, 1, 1), 0.15F);
//            add(Blocks.log2, new ItemStack(Items.coal, 1, 1), 0.15F);
        }
    }
    
    private static void add(Block input, ItemStack stack, float experience)
    {
        add(ItemRegistry.getItemFromBlock(input), stack, experience);
    }
    
    private static void add(Item input, ItemStack stack, float experience)
    {
        add(new ItemStack(input, 1, 32767), 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());
        }
    }
}
