package uk.co.eluinhost.ultrahardcore.config;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import uk.co.eluinhost.ultrahardcore.UltraHardcore;

import java.io.File;
import java.io.InputStream;
import java.util.EnumMap;
import java.util.Map;

public class ConfigManager {

    @SuppressWarnings("UtilityClass")
    private static final class LazyConfigManagerHolder {
        private static final ConfigManager INSTANCE = new ConfigManager();
    }

    /**
     * @return config manager instance
     */
    public static ConfigManager getInstance(){
        return LazyConfigManagerHolder.INSTANCE;
    }

    /**
     * Makes a config manager
     */
    private ConfigManager(){}

    private final Map<ConfigType, FileConfiguration> m_configurations = new EnumMap<ConfigType, FileConfiguration>(ConfigType.class);

    /**
     * Only allows 1 config per configtype
     * @param type the type to add for
     * @param config the config file to save
     */
    public void addConfiguration(ConfigType type, FileConfiguration config){
        m_configurations.put(type,config);
    }

    /**
     * Gets a FileConfiguration from the config directory
     * @param path the file to look for
     * @param setDefaults whether to set the defaults from the same file in the jar
     * @return the config file
     */
    public static FileConfiguration getFromFile(String path, boolean setDefaults){
        UltraHardcore plugin = UltraHardcore.getInstance();
        File configFile = new File(plugin.getDataFolder(), path);
        if (!configFile.exists()) {
            plugin.saveResource(path, false);
        }
        FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
        if(setDefaults){
            // Look for defaults in the jar
            InputStream defConfigStream = plugin.getResource(path);
            if (defConfigStream != null) {
                YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                config.setDefaults(defConfig);
            }
        }
        return config;
    }

    /**
     * @param type the type of config to get
     * @return the configuration or null if not exists
     */
    public FileConfiguration getConfig(ConfigType type) {
        return m_configurations.get(type);
    }

    /**
     * @return the ConfigType.MAIN config file
     */
    public FileConfiguration getConfig(){
        return getConfig(ConfigType.MAIN);
    }

    //TODO wtf is this supposed to be
    public boolean featureEnabledForWorld(String featureNode, String worldName) {
        //w&&f = enabled
        //w&&!f = disabled
        //!w&&f = disabled
        //!w&&!f = enabled
        // = AND
        boolean whitelist = m_configurations.get(ConfigType.MAIN).getBoolean(featureNode + ".whitelist");
        boolean found = m_configurations.get(ConfigType.MAIN).getStringList(featureNode + ".worlds").contains(worldName);
        return !(whitelist ^ found);
    }

    /**
     * Saves the config
     * @param type the config type to save
     */
    public void saveConfig(ConfigType type) {
        FileConfiguration config = m_configurations.get(type);
        if(config != null){
            //TODO thingy to save and stuff
        }
    }
}

@@ -280,16 +280,42 @@
 numMap;%0A
+import java.util.HashMap;%0A
 import j
@@ -363,46 +363,8 @@
  %7B%0A%0A
-    @SuppressWarnings(%22UtilityClass%22)%0A
     
@@ -751,34 +751,30 @@
 e final Map%3C
-ConfigType
+String
 , FileConfig
@@ -809,26 +809,22 @@
 new 
-EnumMap%3CConfigType
+HashMap%3CString
 , Fi
@@ -844,24 +844,8 @@
 on%3E(
-ConfigType.class
 );%0A%0A
@@ -913,32 +913,40 @@
 ram 
-typ
+nam
 e the 
-type to add for
+name of the config file
 %0A   
@@ -1028,30 +1028,26 @@
 uration(
-ConfigType typ
+String nam
 e, FileC
@@ -1097,19 +1097,19 @@
 ons.put(
-typ
+nam
 e,config
@@ -2139,20 +2139,20 @@
 ram 
-typ
+nam
 e the 
-typ
+nam
 e of
@@ -2264,38 +2264,34 @@
 n getConfig(
-ConfigType typ
+String nam
 e) %7B%0A       
@@ -2311,35 +2311,35 @@
 figurations.get(
-typ
+nam
 e);%0A    %7D%0A%0A    /
@@ -2360,31 +2360,22 @@
 urn the 
-ConfigType.MAIN
+'main'
  config 
@@ -2458,538 +2458,14 @@
 fig(
-ConfigType.MAIN);%0A    %7D%0A%0A    //TODO wtf is this supposed to be%0A    public boolean featureEnabledForWorld(String featureNode, String worldName) %7B%0A        //w&&f = enabled%0A        //w&&!f = disabled%0A        //!w&&f = disabled%0A        //!w&&!f = enabled%0A        // = AND%0A        boolean whitelist = m_configurations.get(ConfigType.MAIN).getBoolean(featureNode + %22.whitelist%22);%0A        boolean found = m_configurations.get(ConfigType.MAIN).getStringList(featureNode + %22.worlds%22).contains(worldName);%0A        return !(whitelist %5E found
+%22main%22
 );%0A 
@@ -2516,19 +2516,19 @@
  @param 
-typ
+nam
 e the co
@@ -2536,13 +2536,8 @@
 fig 
-type 
 to s
@@ -2579,22 +2579,18 @@
 fig(
-ConfigType typ
+String nam
 e) %7B
@@ -2646,19 +2646,19 @@
 ons.get(
-typ
+nam
 e);%0A    

change enum map to a string->fileconfiguration hashmap