package fr.noogotte.useful_commands.component;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import fr.aumgn.bukkitutils.gconf.GConfLoadException;
import fr.aumgn.bukkitutils.gconf.GConfLoader;
import fr.noogotte.useful_commands.UsefulCommandPlugin;
import fr.noogotte.useful_commands.component.kit.Kit;

public class KitsComponent extends Component implements Iterable<Entry<String, Kit>> {

    private final Map<String, Kit> kits; 

    public KitsComponent(UsefulCommandPlugin plugin) {
        super(plugin);
        this.kits = new HashMap<String, Kit>();

        File folder = getFolder();
        if (folder == null) {
            return;
        }

        GConfLoader loader = plugin.getGConfLoader();
        for (File file : folder.listFiles()) {
            String name = file.getName();
            if (name.startsWith("template")) {
                continue;
            }

            try {
                Kit kit = loader.loadOrCreate(name, Kit.class);
                kits.put(name, kit);
            } catch (GConfLoadException exc) {
                plugin.getLogger().severe("Unable to read "
                        + name + " kit file.");
            }
        }
    }

    private File getFolder() {
        File folder = new File(plugin.getDataFolder(), "kits");
        if (folder.exists()) {
            if (!folder.isDirectory()) {
                plugin.getLogger().severe(getFolder().getPath()
                        + " is not a directory.");
                return null;
            }
        } else if (!folder.mkdirs()) {
            plugin.getLogger().severe("Unable to create "
                    + folder.getPath() + " directory.");
            return null;
        }

        return folder;
    }

    private String getFilenameFor(String name) {
        return "kits" + File.separator
                + name + ".json";
    }

    private boolean saveKit(String name, Kit kit) {
        GConfLoader loader = plugin.getGConfLoader();
        File folder = getFolder();
        if (folder == null) {
            return false;
        }

        String filename = getFilenameFor(name);
        try {
            loader.write(filename, kit);
            return true;
        } catch (GConfLoadException exc) {
            plugin.getLogger().severe(
                    "Unable to save " + filename + ".");
            return false;
        }
    }

    @Override
    public void onDisable() {
        for (Entry<String, Kit> entry : kits.entrySet()) {
            saveKit(entry.getKey(), entry.getValue());
        }
    }

    public boolean isKit(String name) {
        return kits.containsKey(name);
    }

    public Kit get(String name) {
        return kits.get(name);
    }

    public boolean hasKits() {
        return kits.isEmpty();
    }

    public boolean addKit(String name, Kit kit) {
        boolean success = saveKit(name, kit);
        if (success) {
            kits.put(name, kit);
        }

        return success;
    }

    public boolean removeKit(String name) {
        String filename = getFilenameFor(name);
        File file = new File(
                plugin.getDataFolder(), filename);
        boolean success = file.delete();
        if (success) {
            kits.remove(name);
        }
        return success;
    }

    @Override
    public Iterator<Entry<String, Kit>> iterator() {
        return kits.entrySet().iterator();
    }
}

@@ -848,24 +848,28 @@
      String 
+file
 name = file.
@@ -882,95 +882,8 @@
 e();
-%0A            if (name.startsWith(%22template%22)) %7B%0A                continue;%0A            %7D
 %0A%0A  
@@ -944,16 +944,20 @@
 rCreate(
+file
 name, Ki
@@ -983,36 +983,55 @@
        kits.put(
+getKitNameFor(file
 name
+)
 , kit);%0A        
@@ -1147,32 +1147,36 @@
               + 
+file
 name + %22 kit fil
@@ -1878,32 +1878,294 @@
 %22.json%22;%0A    %7D%0A%0A
+    private String getKitNameFor(String pathname) %7B%0A        int index = pathname.lastIndexOf(File.separatorChar);%0A        String filename = pathname.substring(index + 1);%0A        index = filename.indexOf(%22.%22);%0A        return filename.substring(0, index);%0A    %7D%0A%0A
     private bool

Fix filename to Kit name conversion