package agaricus.mods.miscconfiguration;

import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;

import java.util.*;
import java.util.logging.Level;

@Mod(modid = "MiscConfiguration", name = "MiscConfiguration", version = "1.0-SNAPSHOT") // TODO: version from resource
@NetworkMod(clientSideRequired = false, serverSideRequired = false)
public class MiscConfiguration {

    private List<IRecipe> recipes = new ArrayList<IRecipe>();
    private Map<String, ItemStack> itemNames = new HashMap<String, ItemStack>();

    @Mod.PreInit
    public void preInit(FMLPreInitializationEvent event) {
        Configuration cfg = new Configuration(event.getSuggestedConfigurationFile());

        try {
            cfg.load();

        } catch (Exception e) {
            FMLLog.log(Level.SEVERE, e, "MiscConfiguration had a problem loading it's configuration");
        } finally {
            cfg.save();
        }

        ItemStack output = new ItemStack(12345, 1, 0);
        recipes.add(new ShapelessOreRecipe(output, "ingotCopper")); // TODO: read from config

        FMLLog.log(Level.INFO, "MiscConfiguration enabled");
    }

    @Mod.PostInit
    public void postInit(FMLPostInitializationEvent event) {
        for (IRecipe recipe : recipes) {
            GameRegistry.addRecipe(recipe);
        }
    }

    /**
     * Scan item names for lookup
     */
    private void scanItemNames() {
        // TODO: might have to run at first tick, if this is not late enough for all other mods to be loaded

        FMLLog.log(Level.INFO, "Scanning items");
        for (int id = 0; id < Item.itemsList.length; ++id) {
            Item item = Item.itemsList[id];
            if (item == null) continue;

            int damage = 0; // TODO: scan damages?
            ItemStack itemStack = new ItemStack(id, 1, damage);
            try {
                System.out.println("id "+id+" is "+itemStack.getItemName());
            } catch (Throwable t) {
                ;
            }
        }
        // TODO: cache in itemNames
    }

    private ItemStack itemStackByName(String name) {
        // TODO
        //OreDictionary.getOres(name); // gets a list of all matching entries
        //GameRegistry.findItemStack(modId, name, 1); // need mod id

        return null;
    }
}

@@ -70,16 +70,57 @@
 FMLLog;%0A
+import cpw.mods.fml.common.IFuelHandler;%0A
 import c
@@ -948,18 +948,232 @@
 uration 
-%7B%0A
+implements IFuelHandler %7B%0A%0A    private Map%3CInteger, Integer%3E fuelTimes = new HashMap%3CInteger, Integer%3E();%0A    //private List%3CItemStack, Integer%3E fuelTimes = new ArrayList%3CItemStack, Integer%3E(); // TODO: metadata, NBT
 %0A    pri
@@ -1701,32 +1701,241 @@
      %7D%0A%0A        
+GameRegistry.registerFuelHandler(this);%0A%0A        // TODO%0A        fuelTimes.put(2631,  // Herringbone Parquet Plank%0A                GameRegistry.getFuelValue(new ItemStack(Item.coal.itemID, 1, 0)));%0A%0A        //
 ItemStack output
@@ -1973,16 +1973,18 @@
         
+//
 recipes.
@@ -2307,24 +2307,211 @@
    %7D%0A    %7D%0A%0A
+    @Override%0A    public int getBurnTime(ItemStack fuel) %7B%0A        if (!fuelTimes.containsKey(fuel.itemID)) return 0; // TODO: matching%0A%0A        return fuelTimes.get(fuel.itemID);%0A    %7D%0A%0A
     /**%0A    

Test fuel values