/*
 * DefaultConfigurator.java
 *
 * Copyright (c) 2014 Graham Howden <graham_howden1 at yahoo.co.uk>.
 *
 * This file is part of PluginFramework.
 *
 * PluginFramework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PluginFramework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PluginFramework.  If not, see <http ://www.gnu.org/licenses/>.
 */

package com.publicuhc.configuration;

import com.google.inject.Inject;
import com.publicuhc.configuration.events.ConfigFileReloadedEvent;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class DefaultConfigurator implements Configurator {

    private final Plugin m_plugin;
    private final Map<String, FileConfiguration> m_configs = new HashMap<String, FileConfiguration>();

    @Inject
    public DefaultConfigurator(Plugin plugin) {
        m_plugin = plugin;
    }

    @Override
    public FileConfiguration getConfig(String id) {
        FileConfiguration config = m_configs.get(id);
        if (null == config) {
            config = loadConfig(id);
        }
        return config;
    }

    @Override
    public void saveConfig(String id) {
        FileConfiguration configuration = m_configs.get(id);
        if (configuration != null) {
            try {
                configuration.save(id + ".yml");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public FileConfiguration reloadConfig(String id) {
        FileConfiguration config = loadConfig(id);
        ConfigFileReloadedEvent event = new ConfigFileReloadedEvent(id, config);
        Bukkit.getPluginManager().callEvent(event);
        return config;
    }

    protected FileConfiguration loadConfig(String id) {
        File customConfigFile = new File(m_plugin.getDataFolder(), id+".yml");
        FileConfiguration customConfig = YamlConfiguration.loadConfiguration(customConfigFile);

        // Look for defaults in the jar
        InputStream defConfigStream = m_plugin.getResource(id+".yml");
        if (defConfigStream != null) {
            YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
            customConfig.setDefaults(defConfig);
        }
        m_configs.put(id, customConfig);
        return customConfig;
    }
}

@@ -1083,41 +1083,8 @@
 ion;
-%0Aimport org.bukkit.plugin.Plugin;
 %0A%0Aim
@@ -1158,16 +1158,68 @@
 Stream;%0A
+import java.net.URL;%0Aimport java.net.URLConnection;%0A
 import j
@@ -1323,43 +1323,8 @@
  %7B%0A%0A
-    private final Plugin m_plugin;%0A
     
@@ -1421,16 +1421,53 @@
 tion%3E();
+%0A    private final File m_dataFolder;
 %0A%0A    @I
@@ -1477,21 +1477,24 @@
 ct%0A    p
-ublic
+rotected
  Default
@@ -1510,50 +1510,60 @@
 tor(
-Plugin plugin) %7B%0A        m_plugin = plugin
+File dataFolder) %7B%0A        m_dataFolder = dataFolder
 ;%0A  
@@ -2505,19 +2505,9 @@
 e(m_
-plugin.getD
+d
 ataF
@@ -2511,18 +2511,16 @@
 taFolder
-()
 , id+%22.y
@@ -2704,17 +2704,8 @@
 m = 
-m_plugin.
 getR
@@ -2993,11 +2993,712 @@
 g;%0A    %7D
+%0A%0A    /**%0A     * Load the resource from the plugin%0A     * @param filename the filename to load%0A     * @return inputstream%0A     */%0A    protected InputStream getResource(String filename) %7B%0A        if (filename == null) %7B%0A            throw new IllegalArgumentException(%22Filename cannot be null%22);%0A        %7D%0A%0A        try %7B%0A            URL url = this.getClass().getClassLoader().getResource(filename);%0A%0A            if (url == null) %7B%0A                return null;%0A            %7D%0A%0A            URLConnection connection = url.openConnection();%0A            connection.setUseCaches(false);%0A            return connection.getInputStream();%0A        %7D catch (IOException ex) %7B%0A            return null;%0A        %7D%0A    %7D
 %0A%7D%0A

remove dependency on the plugin