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

import com.elmakers.mine.bukkit.action.BaseProjectileAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.block.MaterialAndData;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.magic.SourceLocation;
import com.elmakers.mine.bukkit.spell.BaseSpell;
import com.elmakers.mine.bukkit.utility.CompatibilityUtils;
import com.elmakers.mine.bukkit.utility.InventoryUtils;
import com.elmakers.mine.bukkit.utility.NMSUtils;
import com.elmakers.mine.bukkit.utility.SafetyUtils;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.util.Vector;

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

public class ThrowItemAction extends BaseProjectileAction {
    private double itemSpeedMin;
    private double itemSpeedMax;
    private int ageItems;
    private boolean unbreakable;
    private SourceLocation sourceLocation;

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters)
    {
        super.prepare(context, parameters);
        double itemSpeed = parameters.getDouble("speed", 1);
        itemSpeedMin = parameters.getDouble("speed_min", itemSpeed);
        itemSpeedMax = parameters.getDouble("speed_max", itemSpeed);
        ageItems = parameters.getInt("age_items", 5500);
        unbreakable = parameters.getBoolean("unbreakable", false);
        sourceLocation = new SourceLocation(parameters);
    }

    @Override
    public SpellResult start(CastContext context)
    {
        MaterialAndData material = context.getBrush();
        Location spawnLocation = sourceLocation.getLocation(context);
        if (spawnLocation == null || material == null)
        {
            return SpellResult.NO_TARGET;
        }
        double itemSpeed = context.getRandom().nextDouble() * (itemSpeedMax - itemSpeedMin) + itemSpeedMin;
        Vector velocity = spawnLocation.getDirection().normalize().multiply(itemSpeed);
        ItemStack itemStack = new ItemStack(material.getMaterial(), 1, material.getData());
        String removedMessage = context.getMessage("removed");
        if (removedMessage != null) {
            String name = material.getName();
            if (name == null) {
                name = "";
            }
            removedMessage = removedMessage.replace("$material", name);
        }
        NMSUtils.makeTemporary(itemStack, removedMessage);
        if (unbreakable) {
            itemStack = InventoryUtils.makeReal(itemStack);
            InventoryUtils.makeUnbreakable(itemStack);
        }
        Item droppedItem = null;
        try {
            droppedItem = spawnLocation.getWorld().dropItem(spawnLocation, itemStack);
        } catch (Exception ex) {

        }
        if (droppedItem == null) {
            context.getMage().sendDebugMessage("Failed to spawn item of type " + itemStack.getType());
            return SpellResult.FAIL;
        }
        droppedItem.setMetadata("temporary", new FixedMetadataValue(context.getController().getPlugin(), true));
        CompatibilityUtils.ageItem(droppedItem, ageItems);
        SafetyUtils.setVelocity(droppedItem, velocity);

        track(context, droppedItem);
        return checkTracking(context);
    }

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

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

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

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

@@ -219,16 +219,67 @@
 ndData;%0A
+import com.elmakers.mine.bukkit.api.item.ItemData;%0A
 import c
@@ -315,24 +315,24 @@
 pell.Spell;%0A
-
 import com.e
@@ -1209,16 +1209,43 @@
 ocation;
+%0A    private ItemData item;
 %0A%0A    @O
@@ -1755,24 +1755,208 @@
 parameters);
+%0A%0A        String itemName = parameters.getString(%22item%22);%0A        if (itemName != null && !itemName.isEmpty()) %7B%0A            item = context.getController().getItem(itemName);%0A        %7D
 %0A    %7D%0A%0A    
@@ -2025,63 +2025,8 @@
   %7B%0A
-        MaterialAndData material = context.getBrush();%0A
     
@@ -2128,20 +2128,430 @@
 null
- %7C%7C material
+)%0A        %7B%0A            return SpellResult.NO_TARGET;%0A        %7D%0A        ItemStack itemStack = null;%0A        if (item != null) %7B%0A            itemStack = item.getItemStack(1);%0A        %7D else %7B%0A            MaterialAndData material = context.getBrush();%0A            if (material != null) %7B%0A                itemStack = new ItemStack(material.getMaterial(), 1, material.getData());%0A            %7D%0A        %7D%0A%0A        if (itemStack
  == 
@@ -2818,100 +2818,8 @@
 d);%0A
-        ItemStack itemStack = new ItemStack(material.getMaterial(), 1, material.getData());%0A
     
@@ -2945,25 +2945,54 @@
 e = 
-material.getName(
+context.getController().describeItem(itemStack
 );%0A 

Add "item" parameter to ThrowItem action