package de.slikey.effectlib.util;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class ConfigUtils {
    public static Collection<ConfigurationSection> getNodeList(ConfigurationSection node, String path) {
        Collection<ConfigurationSection> results = new ArrayList<ConfigurationSection>();
        List<Map<?, ?>> mapList = node.getMapList(path);
        for (Map<?, ?> map : mapList) {
            results.add(toConfigurationSection(map));
        }

        return results;
    }

    @Deprecated
    public static ConfigurationSection toNodeList(Map<?, ?> nodeMap) {
        return toConfigurationSection(nodeMap);
    }

    public static ConfigurationSection toConfigurationSection(Map<?, ?> nodeMap) {
        ConfigurationSection newSection = new MemoryConfiguration();
        for (Map.Entry<?, ?> entry : nodeMap.entrySet()) {
            set(newSection, entry.getKey().toString(), entry.getValue());
        }

        return newSection;
    }

    public static void set(ConfigurationSection node, String path, Object value)
    {
        if (value == null) {
            node.set(path, null);
            return;
        }
        // This is a bunch of hackery... I suppose I ought to change the NBT
        // types to match and make this smarter?
        boolean isTrue = value.equals("true");
        boolean isFalse = value.equals("false");
        if (isTrue || isFalse) {
            node.set(path, isTrue);
        } else {
            try {
                Double d;
                if (value instanceof Double) {
                    d = (Double)value;
                } else if (value instanceof Float) {
                    d = (double)(Float)value;
                } else {
                    d = Double.parseDouble(value.toString());
                }
                node.set(path, d);
            } catch (Exception ex) {
                try {
                    Integer i = (value instanceof Integer) ? (Integer)value : Integer.parseInt(value.toString());
                    node.set(path, i);
                } catch (Exception ex2) {
                    node.set(path, value);
                }
            }
        }
    }

    public static ConfigurationSection getConfigurationSection(ConfigurationSection base, String key)
    {
        ConfigurationSection section = base.getConfigurationSection(key);
        if (section != null) {
            return section;
        }
        Object value = base.get(key);
        if (value == null) return null;

        if (value instanceof ConfigurationSection)
        {
            return (ConfigurationSection)value;
        }

        if (value instanceof Map)
        {
            ConfigurationSection newChild = base.createSection(key);
            @SuppressWarnings("unchecked")
            Map<String, Object> map = (Map<String, Object>)value;
            for (Map.Entry<String, Object> entry : map.entrySet())
            {
                newChild.set(entry.getKey(), entry.getValue());
            }
            base.set(key, newChild);
            return newChild;
        }

        return null;
    }
}

@@ -1310,133 +1310,8 @@
   %7D%0A
-        // This is a bunch of hackery... I suppose I ought to change the NBT%0A        // types to match and make this smarter?
 %0A   
@@ -1499,32 +1499,240 @@
           try %7B%0A
+                Integer i = (value instanceof Integer) ? (Integer)value : Integer.parseInt(value.toString());%0A                node.set(path, i);%0A            %7D catch (Exception ex) %7B%0A                try %7B%0A    
                 
@@ -1741,16 +1741,20 @@
 uble d;%0A
+    
         
@@ -1808,24 +1808,28 @@
             
+    
 d = (Double)
@@ -1847,24 +1847,28 @@
             
+   
+ 
 %7D else if (v
@@ -1908,24 +1908,28 @@
             
+    
 d = (double)
@@ -1950,32 +1950,36 @@
                 
+  
+  
 %7D else %7B%0A       
@@ -1963,32 +1963,36 @@
        %7D else %7B%0A
+    
                 
@@ -2056,218 +2056,14 @@
     
- 
-%7D%0A                node.set(path, d);%0A            %7D catch (Exception ex) %7B%0A                try %7B%0A                    Integer i = (value instanceof Integer) ? (Integer)value : Integer.parseInt(value.toString());
+     %7D
 %0A   
@@ -2086,33 +2086,33 @@
  node.set(path, 
-i
+d
 );%0A             

Fix parsing of integers when translating String parameters