package me.otisdiver.otisarena.utils;

import java.util.Set;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;

import me.otisdiver.otisarena.OtisArena;

public class ConfigUtils {
    
    private static OtisArena main;
    
    private static boolean initiated = false;
    
    public static void initiate(OtisArena mainClass) {
        
        if (initiated) return;
        
        // save the main class
        main = mainClass;
        
        // create a config if needed
        main.saveDefaultConfig();
        
        // flag class as initiated
        initiated = true;
    }
    
    public static void reload() {
        main.reloadConfig();
    }
    
    public static FileConfiguration getConfig() {
        return main.getConfig();
    }
    
    private static String randomKey(Set<String> set) {
        
        // choose a random index, then find it from the set
        int random = RandUtils.rand(set.size());
        int i = 0;
        for(String item : set) {
            if (i == random) return item;
            i++;
        }
        
        // dead code (unless I did something stupid)
        return null;
    }
    
    public static String getRandomWorld() {
        
        // get all items under the section worlds
        Set<String> worlds = getConfig().getConfigurationSection("worlds").getKeys(false);
        
        return randomKey(worlds);
    }
    
    public static Location getRandomSpawn(World world) {
        
        // get the config section worlds.[world].spawns
        StringBuilder path = new StringBuilder();
        path.append("worlds.").append(world.getName()).append(".spawns");
        ConfigurationSection locations = getConfig().getConfigurationSection(path.toString());
        
        // find a set of coords from the config
        String spawn = randomKey(locations.getKeys(false));
        
        // build a Location object
        Double x = locations.getDouble(spawn + ".x");
        Double y = locations.getDouble(spawn + ".y");
        Double z = locations.getDouble(spawn + ".z");
        return new Location(world, x, y, z);
    }
    
}

@@ -380,24 +380,117 @@
 false;%0A    %0A
+    /** Artificial constructor.%0A     * %0A     * @param argMain instance of JavaPlugin%0A     */%0A
     public s
@@ -523,30 +523,19 @@
 ena 
-mainClass
+argMain
 ) %7B%0A
-        %0A
     
@@ -582,66 +582,24 @@
     
-// save the main class%0A        main = mainClass
+main = argMain
 ;%0A
-        %0A
     
@@ -628,14 +628,25 @@
  if 
+o
 ne
-eded
+ doesn't exist
 %0A   

clean up ConfigUtils (improve class consistency)