package heufybot.config;

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

import org.yaml.snakeyaml.Yaml;

import heufybot.core.Logger;
import heufybot.core.cap.*;
import heufybot.utils.FileUtils;

public class GlobalConfig 
{
	public enum PasswordType 
	{
		None, ServerPass, NickServPass, SASL;
	}
	
	protected List<CapHandler> capHandlers;
	protected HashMap<String, Object> settings;
	
	public GlobalConfig()
	{
		this.capHandlers = new ArrayList<CapHandler>();
		this.capHandlers.add(new EnablingCapHandler("multi-prefix"));
	}
	
	@SuppressWarnings("unchecked")
	public void loadGlobalConfig(String fileName)
	{
		Yaml yaml = new Yaml();
		if(FileUtils.fileExists(fileName)) 
		{
			String settingsYaml = FileUtils.readFile(fileName);
			settings = (HashMap<String, Object>) yaml.load(settingsYaml);
		} 
		else
		{
			Logger.error("Config", "Config file \"" + fileName + "\" could not be found. Generating default config...");
			
			settings = new HashMap<String, Object>();
			settings.put("nickname", "RE_HeufyBot");
			settings.put("username", "RE_HeufyBot");
			settings.put("realname", "RE_HeufyBot IRC Bot");
			settings.put("server", "irc.foo.bar");
			settings.put("port", 6667);
			settings.put("ssl", false);
			settings.put("password", "");
			settings.put("passwordType", "None");
			settings.put("autoJoin", false);
			settings.put("autoJoinChannels", new ArrayList<String>());
			settings.put("autoNickChange", true);
			settings.put("autoReconnect", false);
			settings.put("reconnectAttempts", 3);
			settings.put("reconnectInterval", 600);
			settings.put("messageDelay", 500);
			settings.put("logPath", "logs");
			settings.put("logPMs", false);
			settings.put("commandPrefix", "~");
			settings.put("modules", new ArrayList<String>());
			settings.put("botAdmins", new ArrayList<String>());
			
			String settingsYaml = yaml.dump(settings);
			FileUtils.writeFile(fileName, settingsYaml);
		}
	}
	
	public String getSettingWithDefault(String setting, String defaultValue)
	{
		if(settings.containsKey(setting))
		{
			return (String) settings.get(setting);
		}
		else
		{
			Logger.error("Config", "The setting \"" + setting + "\" was not found in your global and local configs! Using the default value \"" + defaultValue + "\" instead.");
			return defaultValue;
		}
	}
	
	public int getSettingWithDefault(String setting, int defaultValue)
	{
		if(settings.containsKey(setting))
		{
			if(settings.get(setting) instanceof Integer)
			{
				return (Integer) settings.get(setting);
			}
			Logger.error("Config", "The setting \"" + setting + "\" was invalid! Using the default value \"" + defaultValue + "\" instead.");
		}
		else
		{
			Logger.error("Config", "The setting \"" + setting + "\" was not found in your global and local configs! Using the default value \"" + defaultValue + "\" instead.");
		}
		return defaultValue;
	}
	
	public boolean getSettingWithDefault(String setting, boolean defaultValue)
	{
		if(settings.containsKey(setting))
		{
			if(settings.get(setting) instanceof Boolean)
			{
				return (Boolean) settings.get(setting);
			}
			Logger.error("Config", "The setting \"" + setting + "\" was invalid! Using the default value \"" + defaultValue + "\" instead.");
		}
		else
		{
			Logger.error("Config", "The setting \"" + setting + "\" was not found in your global and local configs! Using the default value \"" + defaultValue + "\" instead.");
		}
		return defaultValue;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<String> getSettingWithDefault(String setting, ArrayList<String> defaultValue)
	{
		if(settings.containsKey(setting))
		{
			if(settings.get(setting) instanceof ArrayList<?>)
			{
				return (ArrayList<String>) settings.get(setting);
			}
			Logger.error("Config", "The setting \"" + setting + "\" was invalid! Using the default value \"" + defaultValue + "\" instead.");
		}
		else
		{
			Logger.error("Config", "The setting \"" + setting + "\" was not found in your global and local configs! Using the default value \"" + defaultValue + "\" instead.");
		}
		return defaultValue;
	}
	
	public PasswordType getSettingWithDefault(String setting, PasswordType defaultValue)
	{
		if(settings.containsKey(setting))
		{
			try
			{
				return PasswordType.valueOf( (String) settings.get(setting));
			}
			catch (IllegalArgumentException e)
			{
				Logger.error("Config", "The setting \"" + setting + "\" was invalid! Using the default value \"" + defaultValue + "\" instead.");
			}
		}
		else
		{
			Logger.error("Config", "The setting \"" + setting + "\" was not found in your global and local configs! Using the default value \"" + defaultValue + "\" instead.");
		}
		return defaultValue;
	}
	
	public HashMap<String, Object> getSettings()
	{
		return settings;
	}

	public List<CapHandler> getCapHandlers() 
	{
		return capHandlers;
	}
}

@@ -73,16 +73,48 @@
 ashMap;%0A
+import java.util.LinkedHashMap;%0A
 import j
@@ -128,16 +128,16 @@
 l.List;%0A
-
 %0Aimport 
@@ -991,24 +991,48 @@
 .%22);%0A%09%09%09%0A%09%09%09
+HashMap%3CString, Object%3E 
 settings = n
@@ -1034,16 +1034,22 @@
 s = new 
+Linked
 HashMap%3C
@@ -1067,16 +1067,20 @@
 ect%3E();%0A
+%09%09%09%0A
 %09%09%09setti
@@ -1912,16 +1912,16 @@
 ));%0A%09%09%09%0A
-
 %09%09%09Strin
@@ -1958,16 +1958,45 @@
 tings);%0A
+%09%09%09this.settings = settings;%0A
 %09%09%09FileU

Make sure the default settings are sorted properly