package uk.co.eluinhost.configuration;

import org.bukkit.ChatColor;
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.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class ConfigManager {

    private final Map<String, FileConfiguration> m_configurations = new HashMap<String, FileConfiguration>();

    private final FileConfiguration m_translation;

    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(){
        FileConfiguration transConfig = getFromFile("translate.yml",true);
        String language = transConfig.getString("language");
        m_translation = getFromFile("translations/"+language+".yml",true);
    }

    /**
     * Get the message, replaces colour codes
     * @param key the key to look for
     * @return the translated message
     */
    public String getMessage(String key){
        return ChatColor.translateAlternateColorCodes('&',m_translation.getString(key));
    }

    /**
     * Only allows 1 config per configtype
     * @param name the name of the config file
     * @param config the config file to save
     */
    public void addConfiguration(String name, FileConfiguration config){
        m_configurations.put(name,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 name the name of config to get
     * @return the configuration or null if not exists
     */
    public FileConfiguration getConfig(String name) {
        return m_configurations.get(name);
    }

    /**
     * @return the 'main' config file
     */
    public FileConfiguration getConfig(){
        return getConfig("main");
    }

    /**
     * Saves the config to a file with the same name + .yml
     * @param name the config to save
     */
    public void saveConfig(String name) {
        FileConfiguration config = m_configurations.get(name);
        if(config != null){
            try {
                config.save(name+".yml");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

@@ -185,51 +185,32 @@
 ort 
-uk.co.e
+org.bukkit.p
 lu
+g
 in
-host.ultrahardcore.UltraHardcore
+.Plugin
 ;%0A%0Ai
@@ -526,17 +526,16 @@
 lation;%0A
-%0A
     priv
@@ -542,189 +542,31 @@
 ate 
-static final class LazyConfigManagerHolder %7B%0A        private static final ConfigManager INSTANCE = new ConfigManager();%0A    %7D%0A%0A    /**%0A     * @return config manager instance%0A     */
+final Plugin m_plugin;%0A
 %0A   
@@ -573,23 +573,16 @@
  public 
-static 
 ConfigMa
@@ -590,154 +590,51 @@
 ager
- getInstance()%7B%0A        return LazyConfigManagerHolder.INSTANCE;%0A    %7D%0A%0A    /**%0A     * Makes a config manager%0A     */%0A    private ConfigManager()%7B
+(Plugin plugin)%7B%0A        m_plugin = plugin;
 %0A   
@@ -1642,15 +1642,8 @@
 lic 
-static 
 File
@@ -1707,68 +1707,8 @@
 s)%7B%0A
-        UltraHardcore plugin = UltraHardcore.getInstance();%0A
     
@@ -1738,16 +1738,18 @@
 ew File(
+m_
 plugin.g
@@ -1819,16 +1819,18 @@
         
+m_
 plugin.s
@@ -2060,16 +2060,18 @@
 tream = 
+m_
 plugin.g

remove plugin dependency