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

import java.util.Arrays;
import java.util.Collection;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Listener;

import com.elmakers.mine.bukkit.action.BaseSpellAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.spell.BaseSpell;

public class ModifyMaxHealthAction extends BaseSpellAction implements Listener {
    /**
     * The max health to be applied with this action.
     */
    private double health = 0.0;

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters) {
        super.prepare(context, parameters);
        health = parameters.getDouble("max_health", 0.0);
    }

    @Override
    public SpellResult perform(CastContext context) {
        Entity target = context.getTargetEntity();

        if (target == null || !(target instanceof LivingEntity)) {
            return SpellResult.NO_TARGET;
        }
        if (health <= 0) {
            return SpellResult.FAIL;
        }

        LivingEntity li = (LivingEntity)target;
        if (li.getHealth() > health) {
            li.setHealth(health);
        }
        li.setMaxHealth(health);

        return SpellResult.CAST;
    }

    @Override
    public boolean isUndoable()
    {
        return false;
    }

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


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

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

@@ -216,32 +216,65 @@
 y.LivingEntity;%0A
+import org.bukkit.entity.Player;%0A
 import org.bukki
@@ -291,16 +291,16 @@
 stener;%0A
-
 %0Aimport 
@@ -741,16 +741,54 @@
 h = 0.0;
+%0A    private double healthScale = 0.0;
 %0A%0A    @O
@@ -977,16 +977,81 @@
 , 0.0);%0A
+        healthScale = parameters.getDouble(%22health_scale%22, 0.0);%0A
     %7D%0A%0A 
@@ -1486,32 +1486,32 @@
 lth);%0A        %7D%0A
-
         li.setMa
@@ -1526,16 +1526,169 @@
 health);
+%0A        if (li instanceof Player && healthScale %3E 0) %7B%0A            Player player = (Player)li;%0A            player.setHealthScale(healthScale);%0A        %7D
 %0A%0A      

Add health_scale parameter to ModifyMaxHealth action