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

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

import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;

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.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.spell.BaseSpell;

public class OrientAction extends BaseSpellAction {
    private Float pitch;
    private Float yaw;
    private Float pitchOffset;
    private Float yawOffset;
    private boolean orientTarget;

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters) {
        super.prepare(context, parameters);
        if (parameters.contains("pitch")) {
            pitch = (float)parameters.getDouble("pitch");
        } else {
            pitch = null;
        }
        if (parameters.contains("yaw")) {
            yaw = (float)parameters.getDouble("yaw");
        } else {
            yaw = null;
        }
        if (parameters.contains("pitch_offset")) {
            pitchOffset = (float)parameters.getDouble("pitch_offset");
        } else {
            pitchOffset = null;
        }
        if (parameters.contains("yaw_offset")) {
            yawOffset = (float)parameters.getDouble("yaw_offset");
        } else {
            yawOffset = null;
        }
        orientTarget = parameters.getBoolean("orient_target", false);
    }

    @Override
    public SpellResult perform(CastContext context) {
        Mage mage = context.getMage();
        Entity entity = orientTarget ? context.getTargetEntity() : mage.getEntity();
        if (entity == null)
        {
            return orientTarget ? SpellResult.NO_TARGET : SpellResult.ENTITY_REQUIRED;
        }

        Location location = entity.getLocation();
        context.registerMoved(entity);
        if (pitch != null || yaw != null)
        {
            if (pitch != null) {
                location.setPitch(pitch);
            }
            if (yaw != null) {
                location.setYaw(yaw);
            }
        }
        if (pitchOffset != null || yawOffset != null)
        {
            if (pitchOffset != null) {
                location.setPitch(location.getPitch() + pitchOffset);
            }
            if (yawOffset != null) {
                location.setYaw(location.getYaw() + yawOffset);
            }
        }
        if (pitchOffset == null && yawOffset == null && yaw == null && pitch == null)
        {
            Entity targetEntity = orientTarget ? mage.getEntity() : context.getTargetEntity();
            if (targetEntity == null)
            {
                return orientTarget ? SpellResult.ENTITY_REQUIRED : SpellResult.NO_TARGET;
            }
            Location direction = targetEntity.getLocation().subtract(location);
            location.setDirection(direction.toVector());
        }
        entity.teleport(location);

        return SpellResult.CAST;
    }

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

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

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

@@ -721,16 +721,49 @@
 tTarget;
+%0A    private boolean targetBlock;
 %0A%0A    @O
@@ -1620,16 +1620,84 @@
 false);%0A
+        targetBlock = parameters.getBoolean(%22target_block%22, false);%0A
     %7D%0A%0A 
@@ -2889,23 +2889,79 @@
 null
-)%0A            %7B
+ && orientTarget) %7B%0A                return SpellResult.ENTITY_REQUIRED;
 %0A   
@@ -2970,66 +2970,340 @@
     
+   %7D%0A
        
-return orientTarget ? SpellResult.ENTITY_REQUIRED :
+     Location targetLocation = null;%0A            if (targetBlock) %7B%0A                targetLocation = context.getTargetLocation();%0A            %7D else %7B%0A                targetLocation = targetEntity == null ? null : targetEntity.getLocation();%0A            %7D%0A            if (targetLocation == null) %7B%0A                return
  Spe
@@ -3368,34 +3368,24 @@
 ection = tar
-getEntity.
 getLocation(
@@ -3383,16 +3383,22 @@
 Location
+.clone
 ().subtr

Allow orienting towards a block, not just an entity