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

import com.elmakers.mine.bukkit.action.BaseSpellAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.magic.MageController;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.spell.BaseSpell;
import com.elmakers.mine.bukkit.utility.CompatibilityUtils;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;

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

public class DamageAction extends BaseSpellAction
{
    private double entityDamage;
    private double playerDamage;
    private double elementalDamage;
    private boolean magicDamage;

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters)
    {
        super.prepare(context, parameters);
        double damage = parameters.getDouble("damage", 1);
        entityDamage = parameters.getDouble("entity_damage", damage);
        playerDamage = parameters.getDouble("player_damage", damage);
        elementalDamage = parameters.getDouble("elemental_damage", damage);
        magicDamage = parameters.getBoolean("magic_damage", true);
    }

	@Override
	public SpellResult perform(CastContext context)
	{
        Entity entity = context.getTargetEntity();
		if (entity != null && !(entity instanceof LivingEntity))
		{
			return SpellResult.NO_TARGET;
		}

		double damage = 1;

        LivingEntity targetEntity = (LivingEntity)entity;
        context.registerModified(targetEntity);
		Mage mage = context.getMage();
		MageController controller = context.getController();

		if (controller.isElemental(entity)) {
			damage = elementalDamage;
			controller.damageElemental(entity, damage * mage.getDamageMultiplier(), 0, mage.getCommandSender());
		} else {
			if (targetEntity instanceof Player) {
				damage = playerDamage;
			} else {
				damage = entityDamage;
			}
            damage *= mage.getDamageMultiplier();
            if (magicDamage) {
                CompatibilityUtils.magicDamage(targetEntity, damage, mage.getEntity());
            } else {
                targetEntity.damage(damage, mage.getEntity());
            }
		}

		return SpellResult.CAST;
	}

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

	@Override
	public void getParameterNames(Spell spell, Collection<String> parameters) {
		super.getParameterNames(spell, parameters);
		parameters.add("damage");
		parameters.add("player_damage");
		parameters.add("entity_damage");
		parameters.add("elemental_damage");
        parameters.add("magic_damage");
	}

	@Override
	public void getParameterOptions(Spell spell, String parameterKey, Collection<String> examples) {
		if (parameterKey.equals("damage") || parameterKey.equals("player_damage")
			|| parameterKey.equals("entity_damage") || parameterKey.equals("elemental_damage")) {
			examples.addAll(Arrays.asList((BaseSpell.EXAMPLE_SIZES)));
		} else if (parameterKey.equals("magic_damage")) {
            examples.addAll(Arrays.asList((BaseSpell.EXAMPLE_BOOLEANS)));
        } else {
			super.getParameterOptions(spell, parameterKey, examples);
		}
	}

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

@@ -875,16 +875,44 @@
 cDamage;
+%0A%09private Double percentage;
 %0A%0A    @O
@@ -1314,32 +1314,164 @@
 mage%22, damage);%0A
+%09%09if (parameters.contains(%22percentage%22)) %7B%0A%09%09%09percentage = parameters.getDouble(%22percentage%22);%0A%09%09%7D else %7B%0A%09%09%09percentage = null;%0A%09%09%7D%0A
         magicDam
@@ -2150,16 +2150,107 @@
 %7B%0A%09%09%09if 
+(percentage != null) %7B%0A%09%09%09%09damage = percentage * targetEntity.getMaxHealth();%0A%09%09%09%7D else if 
 (targetE
@@ -3022,16 +3022,48 @@
 mage%22);%0A
+%09%09parameters.add(%22percentage%22);%0A
 %09%7D%0A%0A%09@Ov
@@ -3516,24 +3516,24 @@
 OOLEANS)));%0A
-
         %7D el
@@ -3521,32 +3521,150 @@
 NS)));%0A        %7D
+ else if (parameterKey.equals(%22percentage%22)) %7B%0A%09%09%09examples.addAll(Arrays.asList((BaseSpell.EXAMPLE_PERCENTAGES)));%0A%09%09%7D
  else %7B%0A%09%09%09super

Add percentage parameter to Damage action