package com.github.lunatrius.ingameinfo.handler;

import com.github.lunatrius.ingameinfo.Alignment;
import com.github.lunatrius.ingameinfo.lib.Reference;
import cpw.mods.fml.client.event.ConfigChangedEvent;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.common.config.Configuration;
import net.minecraftforge.common.config.Property;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class ConfigurationHandler {
	public static final String CATEGORY_GENERAL = "general";
	public static final String CATEGORY_ALIGNMENT = "alignment";

	public static final String FILENAME = "filename";
	public static final String FILENAME_DESC = "The configuration that that should be loaded on startup.";

	public static final String REPLACEDEBUG = "replaceDebug";
	public static final String REPLACEDEBUG_DESC = "Replace the debug overlay (F3) with the InGameInfoXML overlay.";

	public static final String SHOWINCHAT = "showInChat";
	public static final String SHOWINCHAT_DESC = "Display the overlay in chat.";

	public static final String SHOWONPLAYERLIST = "showOnPlayerList";
	public static final String SHOWONPLAYERLIST_DESC = "Display the overlay on the player list.";

	public static final String ALIGNMENT_DESC = "Offsets for %s (X<space>Y).";
	public static final String LANG_PREFIX = Reference.MODID.toLowerCase() + ".config";

	public static Configuration configuration;

	public static String configName = "InGameInfo.xml";
	// TODO: 1.8 - flip the default to true
	public static boolean replaceDebug = false;
	public static boolean showInChat = true;
	public static boolean showOnPlayerList = true;

	private static Property propConfigName = null;
	private static Property propReplaceDebug = null;
	private static Property propShowInChat = null;
	private static Property propShowOnPlayerList = null;
	private static final Map<Alignment, Property> propAlignments = new HashMap<Alignment, Property>();

	public static void init(File configFile) {
		if (configuration == null) {
			configuration = new Configuration(configFile);
			loadConfiguration();
		}
	}

	private static void loadConfiguration() {
		propConfigName = configuration.get(CATEGORY_GENERAL, FILENAME, "InGameInfo.xml", FILENAME_DESC);
		propConfigName.setLanguageKey(String.format("%s.%s", LANG_PREFIX, FILENAME));
		propConfigName.setRequiresMcRestart(true);
		configName = propConfigName.getString();

		propReplaceDebug = configuration.get(CATEGORY_GENERAL, REPLACEDEBUG, replaceDebug, REPLACEDEBUG_DESC);
		propReplaceDebug.setLanguageKey(String.format("%s.%s", LANG_PREFIX, REPLACEDEBUG));
		replaceDebug = propReplaceDebug.getBoolean(replaceDebug);

		propShowInChat = configuration.get(CATEGORY_GENERAL, SHOWINCHAT, showInChat, SHOWINCHAT_DESC);
		propShowInChat.setLanguageKey(String.format("%s.%s", LANG_PREFIX, SHOWINCHAT));
		showInChat = propShowInChat.getBoolean(showInChat);

		propShowOnPlayerList = configuration.get(CATEGORY_GENERAL, SHOWONPLAYERLIST, showOnPlayerList, SHOWONPLAYERLIST_DESC);
		propShowOnPlayerList.setLanguageKey(String.format("%s.%s", LANG_PREFIX, SHOWONPLAYERLIST));
		showOnPlayerList = propShowOnPlayerList.getBoolean(showOnPlayerList);

		for (Alignment alignment : Alignment.values()) {
			Property property = configuration.get(CATEGORY_ALIGNMENT, alignment.toString().toLowerCase(), alignment.getXY(), String.format(ALIGNMENT_DESC, alignment.toString()));
			property.setLanguageKey(String.format("%s.%s", LANG_PREFIX, alignment.toString().toLowerCase()));
			property.setValidationPattern(Pattern.compile("-?\\d+ -?\\d+"));
			propAlignments.put(alignment, property);
			alignment.setXY(property.getString());
		}

		save();
	}

	public static void reload() {
		loadConfiguration();
		save();
	}

	public static void save() {
		if (configuration.hasChanged()) {
			configuration.save();
		}
	}

	public static void setConfigName(String name) {
		propConfigName.set(name);
	}

	@SubscribeEvent
	public void onConfigurationChangedEvent(ConfigChangedEvent.OnConfigChangedEvent event) {
		if (event.modID.equalsIgnoreCase(Reference.MODID)) {
			loadConfiguration();
		}
	}
}

@@ -1474,25 +1474,39 @@
 tic 
+final 
 String 
-configName
+CONFIGNAME_DEFAULT
  = %22
@@ -1578,24 +1578,30 @@
  static 
+final 
 boolean 
 replaceD
@@ -1596,28 +1596,270 @@
 ean 
-replaceDebug = false
+REPLACEDEBUG_DEFAULT = false;%0A%09public static final boolean SHOWINCHAT_DEFAULT = true;%0A%09public static final boolean SHOWONPLAYERLIST_DEFAULT = true;%0A%0A%09public static String configName = CONFIGNAME_DEFAULT;%0A%09public static boolean replaceDebug = REPLACEDEBUG_DEFAULT
 ;%0A%09p
@@ -1892,20 +1892,34 @@
 nChat = 
-true
+SHOWINCHAT_DEFAULT
 ;%0A%09publi
@@ -1954,20 +1954,40 @@
 rList = 
-true
+SHOWONPLAYERLIST_DEFAULT
 ;%0A%0A%09priv
@@ -2547,32 +2547,34 @@
 LENAME, 
-%22InGameInfo.xml%22
+CONFIGNAME_DEFAULT
 , FILENA
@@ -2823,28 +2823,36 @@
 EDEBUG, 
-replaceDebug
+REPLACEDEBUG_DEFAULT
 , REPLAC
@@ -2996,28 +2996,36 @@
 Boolean(
-replaceDebug
+REPLACEDEBUG_DEFAULT
 );%0A%0A%09%09pr
@@ -3087,26 +3087,34 @@
 INCHAT, 
-showInChat
+SHOWINCHAT_DEFAULT
 , SHOWIN
@@ -3248,26 +3248,34 @@
 Boolean(
-showInChat
+SHOWINCHAT_DEFAULT
 );%0A%0A%09%09pr
@@ -3349,32 +3349,40 @@
 ERLIST, 
-showOnPlayerList
+SHOWONPLAYERLIST_DEFAULT
 , SHOWON
@@ -3546,32 +3546,40 @@
 Boolean(
-showOnPlayerList
+SHOWONPLAYERLIST_DEFAULT
 );%0A%0A%09%09fo

Make the configuration defaults proper, final defaults.