package org.cyclops.cyclopscore.client.gui.config;

import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.common.config.ConfigElement;
import net.minecraftforge.fml.client.config.DummyConfigElement;
import net.minecraftforge.fml.client.config.GuiConfig;
import net.minecraftforge.fml.client.config.GuiConfigEntries;
import net.minecraftforge.fml.client.config.GuiConfigEntries.CategoryEntry;
import net.minecraftforge.fml.client.config.IConfigElement;
import org.cyclops.cyclopscore.config.ConfigHandler;
import org.cyclops.cyclopscore.config.ConfigurableTypeCategory;
import org.cyclops.cyclopscore.init.ModBase;

import java.util.ArrayList;
import java.util.List;

/**
 * Overview config screen.
 * Extend this class and make sure that this new class has a constructor with single parameter {@link GuiScreen}
 * @author rubensworks
 *
 */
public abstract class GuiConfigOverviewBase extends GuiConfig {

    private final ModBase mod;

	/**
	 * Make a new instance.
	 * @param mod The mod.
	 * @param parentScreen the parent GuiScreen object
	 */
	public GuiConfigOverviewBase(ModBase mod, GuiScreen parentScreen) {
		super(parentScreen, getConfigElements(mod), mod.getModId(), false, false,
			GuiConfig.getAbridgedConfigPath(mod.getConfigHandler().getConfig().toString()));
        this.mod = mod;
	}

	public ModBase getMod() {
        return this.mod;
    }

	private ConfigHandler getConfigHandler() {
		return getMod().getConfigHandler();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private static List<IConfigElement> getConfigElements(ModBase mod) {
		List<IConfigElement> list = new ArrayList<IConfigElement>();
		for(String category : mod.getConfigHandler().getCategories()) {
			list.add(new DummyConfigElement.DummyCategoryElement(category, category, ExtendedCategoryEntry.class));
		}
		return list;
	}
	
	/**
	 * A category entry for {@link ConfigurableTypeCategory}.
	 * @author rubensworks
	 *
	 */
	public class ExtendedCategoryEntry extends CategoryEntry {

		private String category;
		
		/**
		 * Make a new instance.
		 * @param config The config gui.
		 * @param entries The gui entries.
		 * @param element The config element for this category.
		 */
		@SuppressWarnings("rawtypes")
		public ExtendedCategoryEntry(GuiConfig config, GuiConfigEntries entries,
				IConfigElement element) {
			super(config, entries, element);			
		}
		
		@SuppressWarnings({ "unchecked", "rawtypes" })
		@Override
        protected GuiScreen buildChildScreen() {
			// Cheaty way of getting the current ConfigurableTypeCategory.
			this.category = configElement.getName();
			
			// Get all the elements inside this category
            List<IConfigElement> elements = (new ConfigElement(getConfigHandler().getConfig()
            		.getCategory(category))).getChildElements();
			return new GuiConfig(this.owningScreen, elements,
                    this.owningScreen.modID, category,
                    this.configElement.requiresWorldRestart()
                    	|| this.owningScreen.allRequireWorldRestart, 
                    this.configElement.requiresMcRestart()
                    	|| this.owningScreen.allRequireMcRestart,
                    GuiConfig.getAbridgedConfigPath(getConfigHandler().getConfig().toString()));
        }
		
	}
	
}

@@ -464,61 +464,8 @@
 nt;%0A
-import org.cyclops.cyclopscore.config.ConfigHandler;%0A
 impo
@@ -520,24 +520,24 @@
 peCategory;%0A
+
 import org.c
@@ -1324,94 +1324,8 @@
  %7D%0A%0A
-%09private ConfigHandler getConfigHandler() %7B%0A%09%09return getMod().getConfigHandler();%0A%09%7D%0A%0A
 %09@Su
@@ -1803,16 +1803,23 @@
 %09public 
+static 
 class Ex
@@ -2233,22 +2233,25 @@
 lement);
-%09%09%09%0A%09%09
+%0A        
 %7D%0A%09%09%0A%09%09@
@@ -2357,16 +2357,97 @@
 een() %7B%0A
+            ModBase mod = ((GuiConfigOverviewBase) this.owningScreen).getMod();%0A%0A
 %09%09%09// Ch
@@ -2552,15 +2552,20 @@
 ();%0A
-%09%09%09%0A%09%09%09
+            
 // G
@@ -2668,16 +2668,20 @@
 Element(
+mod.
 getConfi
@@ -3121,16 +3121,16 @@
 estart,%0A
-
         
@@ -3173,16 +3173,20 @@
 figPath(
+mod.
 getConfi

Fix NPE's when opening ingame config