package com.elmakers.mine.bukkit.effect.builtin;

import com.elmakers.mine.bukkit.effect.EffectPlayer;
import org.bukkit.Color;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import org.bukkit.plugin.Plugin;

import java.util.*;

public class EffectVariable extends EffectPlayer {

    private Map<Double, Collection<EffectPlayer>> brightnessMap = new TreeMap<Double, Collection<EffectPlayer>>();
    private Collection<EffectPlayer> playing = new ArrayList<EffectPlayer>();

    public EffectVariable() {

    }

    @Override
    public void cancel() {
        super.cancel();
        for (EffectPlayer player : playing) {
            player.cancel();;
        }
    }

    @Override
    public void load(Plugin plugin, ConfigurationSection configuration) {
        super.load(plugin, configuration);

        // ... This seems kinda broken. I get a Map here if it's embedded in another section?
        Object testObject = configuration.get("brightness");
        if (testObject != null) {
            ConfigurationSection brightness = null;
            if (testObject instanceof Map) {
                Map<String, List<ConfigurationSection>> dataMap = (Map<String, List<ConfigurationSection>>)testObject;
                brightness = new MemoryConfiguration();
                for (Map.Entry<String, List<ConfigurationSection>> entry : dataMap.entrySet()) {
                    brightness.set(entry.getKey(), entry.getValue());
                }
            } else {
                brightness = configuration.getConfigurationSection("brightness");
            }
            if (brightness != null) {
                Collection<String> keys = brightness.getKeys(false);
                for (String key : keys) {
                    try {
                        double level = Double.parseDouble(key);
                        brightnessMap.put(level, EffectPlayer.loadEffects(plugin, brightness, key));
                    } catch (Exception ex) {

                    }
                }
            }
        }
    }

    public void play() {
        playing.clear();
        if (brightnessMap.size() > 0) {
            double brightness = 0;
            Color color = getColor1();
            if (color != null) {
                brightness = (double) color.getRed() / 255.0 + (double) color.getBlue() / 255.0 + (double) color.getGreen() / 255.0;
            }
            for (Map.Entry<Double, Collection<EffectPlayer>> entry : brightnessMap.entrySet()) {
                if (brightness < entry.getKey()) {
                    for (EffectPlayer player : entry.getValue()) {
                        // Set scale
                        player.setScale(scale);

                        // Set material and color
                        player.setMaterial(getWorkingMaterial());
                        player.setColor(getColor1());
                        player.setParticleOverride(particleOverride);
                        player.start(origin, originEntity == null ? null : originEntity.get(),
                                target, targetEntity == null ? null : targetEntity.get());

                        playing.add(player);
                    }
                    break;
                }
            }
        }
    }
}

@@ -706,17 +706,16 @@
 ancel();
-;
 %0A       
@@ -1049,60 +1049,8 @@
 ) %7B%0A
-            ConfigurationSection brightness = null;%0A
     
@@ -1213,64 +1213,8 @@
 ct;%0A
-                brightness = new MemoryConfiguration();%0A
     
@@ -1330,38 +1330,50 @@
     
-brightness.set(entry.getKey(),
+try %7B%0A                        String key =
  ent
@@ -1378,23 +1378,20 @@
 ntry.get
-Value()
+Key(
 );%0A     
@@ -1397,34 +1397,32 @@
             
-%7D%0A
         
     %7D else %7B
@@ -1413,36 +1413,91 @@
     
-    %7D else %7B%0A               
+double level = Double.parseDouble(key);%0A                        MemoryConfiguration
  bri
@@ -1506,17 +1506,27 @@
 tness = 
-c
+new MemoryC
 onfigura
@@ -1533,78 +1533,319 @@
 tion
-.getConfigurationSection(%22brightness%22);%0A            %7D%0A            if (
+();%0A                        brightness.set(key, entry.getValue());%0A                        brightnessMap.put(level, EffectPlayer.loadEffects(plugin, brightness, key));%0A                    %7D catch (Exception ex) %7B%0A                    %7D%0A                %7D%0A            %7D else %7B%0A                ConfigurationSection 
 brig
@@ -1851,26 +1851,70 @@
 ghtness 
-!
 = 
-null) %7B
+configuration.getConfigurationSection(%22brightness%22);
 %0A       
@@ -2249,25 +2249,24 @@
 ption ex) %7B%0A
-%0A
             

Fix variable FX