package com.elmakers.mine.bukkit.effect;

import com.elmakers.mine.bukkit.block.MaterialAndData;
import de.slikey.effectlib.Effect;
import de.slikey.effectlib.EffectManager;
import de.slikey.effectlib.util.DynamicLocation;
import de.slikey.effectlib.util.ParticleEffect;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemoryConfiguration;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * Manages EffectLib integration
 */
public class EffectLibManager {
    private static EffectManager effectManager;

    public EffectLibManager() {
    }

    public static EffectLibManager initialize(Plugin plugin) {
        if (effectManager == null) {
            effectManager = new EffectManager(plugin);
        }

        return new EffectLibManager();
    }

    public void enableDebug(boolean debug) {
        if (effectManager != null) {
            effectManager.enableDebug(debug);
        }
    }

    public boolean isDebugEnabled() {
        return effectManager != null ? effectManager.isDebugEnabled() : false;
    }

    public void setParticleRange(int range) {
        if (effectManager != null) {
            effectManager.setParticleRange(range);
        }
    }

    public Effect play(ConfigurationSection configuration, EffectPlayer player, DynamicLocation origin, DynamicLocation target, Map<String, String> parameterMap) {
        if (parameterMap == null) {
            parameterMap = new HashMap<String, String>();
        }
        Entity originEntity = origin == null ? null : origin.getEntity();
        if (originEntity != null && originEntity instanceof Player) {
            parameterMap.put("$name", ((Player)originEntity).getName());
        } else if (originEntity != null && originEntity instanceof LivingEntity) {
            parameterMap.put("$name", ((LivingEntity)originEntity).getCustomName());
        } else {
            parameterMap.put("$name", "Unknown");
        }
        Entity targetEntity = target == null ? null : target.getEntity();
        if (targetEntity != null && targetEntity instanceof Player) {
            parameterMap.put("$target", ((Player)targetEntity).getName());
        } else if (originEntity != null && targetEntity instanceof LivingEntity) {
            parameterMap.put("$target", ((LivingEntity)targetEntity).getCustomName());
        } else {
            parameterMap.put("$target", "Unknown");
        }

        Effect effect = null;
        String effectClass = configuration.getString("class");
        ParticleEffect particleEffect = player.overrideParticle(null);
        String effectOverride = player.getParticleOverrideName();
        if (effectOverride != null && effectOverride.isEmpty()) effectOverride = null;
        String colorOverrideName = player.getColorOverrideName();
        if (colorOverrideName != null && colorOverrideName.isEmpty()) colorOverrideName = null;
        ConfigurationSection parameters = configuration;
        Color colorOverride = player.getColor1();
        if ((colorOverrideName != null && colorOverride != null) || (effectOverride != null && particleEffect != null))
        {
            parameters = new MemoryConfiguration();
            Collection<String> keys = configuration.getKeys(false);
            for (String key : keys) {
                parameters.set(key, configuration.get(key));
            }
            if (effectOverride != null && particleEffect != null)
            {
                parameters.set(effectOverride, particleEffect.name());
            }
            if (colorOverride != null && colorOverrideName != null)
            {
                String hexColor = Integer.toHexString(colorOverride.asRGB());
                parameters.set(colorOverrideName, hexColor);
            }
        }
        MaterialAndData mat = player.getWorkingMaterial();
        if (!parameters.contains("material"))
        {
            parameters.set("material", mat.getMaterial().name());
            parameters.set("materialData", mat.getData());
        }

        try {
            effect = effectManager.start(effectClass, parameters, origin, target, parameterMap);
        } catch (Throwable ex) {
            Bukkit.getLogger().warning("Error playing effects of class: " + effectClass);
            ex.printStackTrace();
        }
        return effect;
    }

    public void cancel(Collection<Effect> effects) {
        for (Effect effect : effects) {
            try {
                effect.cancel();
            } catch (Throwable ex) {
                Bukkit.getLogger().warning("Error cancelling effects");
                ex.printStackTrace();
            }
        }
    }
}

@@ -3990,24 +3990,25 @@
       %7D%0A
+%0A
         
 Material
@@ -4003,59 +4003,115 @@
     
-MaterialAndData mat = player.getWorkingMaterial();%0A
+try %7B%0A            effect = effectManager.start(effectClass, parameters, origin, target, parameterMap);%0A    
     
@@ -4152,32 +4152,36 @@
 rial%22))%0A        
+    
 %7B%0A            pa
@@ -4182,42 +4182,51 @@
     
-parameters.set(%22material%22, mat.get
+    MaterialAndData mat = player.getWorking
 Mate
@@ -4231,24 +4231,16 @@
 terial()
-.name())
 ;%0A      
@@ -4249,32 +4249,27 @@
     
-parameters.set(%22
+    effect.
 material
 Data
@@ -4268,22 +4268,18 @@
 rial
-Data%22,
+ =
  mat.get
 Data
@@ -4278,14 +4278,17 @@
 .get
-Data()
+Material(
 );%0A 
@@ -4298,121 +4298,71 @@
     
-%7D%0A%0A        try %7B%0A            effect = effectManager.start(effectClass, parameters, origin, target, parameterMap);
+        effect.materialData = mat.getBlockData();%0A            %7D
 %0A   

Fix effectlib material overrides getting stuck on the first material