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

import com.elmakers.mine.bukkit.action.CompoundEntityAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.spell.BaseSpell;
import com.elmakers.mine.bukkit.utility.CompatibilityUtils;
import com.elmakers.mine.bukkit.utility.Target;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class AreaOfEffectAction extends CompoundEntityAction
{
    private int radius;
    private int targetCount;

    @Override
    public void reset(CastContext context) {
        super.reset(context);
        createActionContext(context, context.getTargetEntity(), context.getTargetLocation());
    }

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters)
    {
        radius = parameters.getInt("radius", 8);
        targetCount = parameters.getInt("target_count", -1);

        Mage mage = context.getMage();
        if (mage != null)
        {
            radius = (int)(mage.getRadiusMultiplier() * radius);
        }

        super.prepare(context, parameters);
    }

    @Override
    public void addEntities(CastContext context, List<WeakReference<Entity>> entities)
    {
        Entity sourceEntity = context.getMage().getEntity();
        Location sourceLocation = context.getTargetLocation();
        List<Entity> candidates = CompatibilityUtils.getNearbyEntities(sourceLocation, radius, radius, radius);
        if (targetCount > 0)
        {
            List<Target> targets = new ArrayList<Target>();
            for (Entity entity : candidates)
            {
                if ((context.getTargetsCaster() || entity != sourceEntity) && context.canTarget(entity))
                {
                    targets.add(new Target(sourceLocation, entity, radius));
                }
            }
            Collections.sort(targets);
            for (int i = 0; i < targetCount && i < targets.size(); i++)
            {
                Target target = targets.get(i);
                entities.add(new WeakReference<Entity>(target.getEntity()));
            }
        }
        else
        {
            for (Entity entity : candidates)
            {
                if ((context.getTargetsCaster() || !entity.equals(sourceEntity)) && context.canTarget(entity))
                {
                    entities.add(new WeakReference<Entity>(entity));
                }
            }
        }
    }

    @Override
    public void getParameterNames(Spell spell, Collection<String> parameters) {
        super.getParameterNames(spell, parameters);
        parameters.add("radius");
        parameters.add("target_count");
    }

    @Override
    public void getParameterOptions(Spell spell, String parameterKey, Collection<String> examples) {
        if (parameterKey.equals("target_count") || parameterKey.equals("radius")) {
            examples.addAll(Arrays.asList((BaseSpell.EXAMPLE_SIZES)));
        } else {
            super.getParameterOptions(spell, parameterKey, examples);
        }
    }

    @Override
    public boolean requiresTarget() {
        return true;
    }
}

@@ -414,16 +414,45 @@
 Target;%0A
+import org.bukkit.ChatColor;%0A
 import o
@@ -1548,32 +1548,71 @@
 entities)%0A    %7B%0A
+        Mage mage = context.getMage();%0A
         Entity s
@@ -1715,24 +1715,484 @@
 Location();%0A
+        if (mage.getDebugLevel() %3E 8)%0A        %7B%0A            mage.sendDebugMessage(ChatColor.GREEN + %22AOE Targeting from %22 + ChatColor.GRAY + sourceLocation.getBlockX() +%0A                    ChatColor.DARK_GRAY + %22,%22  + ChatColor.GRAY + sourceLocation.getBlockY() +%0A                    ChatColor.DARK_GRAY + %22,%22 + ChatColor.GRAY + sourceLocation.getBlockZ() +%0A                    ChatColor.DARK_GREEN + %22 with radius of %22 + ChatColor.GREEN + radius);%0A        %7D%0A
         List
@@ -2596,20 +2596,24 @@
     
-t
+T
 arget
-s.add(
+ target = 
 new 
@@ -2650,16 +2650,449 @@
  radius)
+;%0A                    targets.add(target);%0A                    mage.sendDebugMessage(ChatColor.DARK_GREEN + %22Target %22 + ChatColor.GREEN + entity.getType() + ChatColor.DARK_GREEN + %22: %22 + ChatColor.YELLOW + target.getScore(), 6);%0A                %7D%0A                else if (mage.getDebugLevel() %3E 7)%0A                %7B%0A                    mage.sendDebugMessage(ChatColor.DARK_RED + %22Skipped Target %22 + ChatColor.GREEN + entity.getType()
 );%0A     
@@ -3668,24 +3668,348 @@
 %3E(entity));%0A
+                    mage.sendDebugMessage(ChatColor.DARK_GREEN + %22Target %22 + ChatColor.GREEN + entity.getType(), 6);%0A                %7D%0A                else if (mage.getDebugLevel() %3E 7)%0A                %7B%0A                    mage.sendDebugMessage(ChatColor.DARK_RED + %22Skipped Target %22 + ChatColor.GREEN + entity.getType());%0A
             

Add debug output to AOE action