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

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;
import com.elmakers.mine.bukkit.action.BaseSpellAction;
import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;

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

public class OrientAction extends BaseSpellAction {
    private Float pitch;
    private Float yaw;

    @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;
        }
    }

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

        context.registerMoved(entity);
        Location location = entity.getLocation();
        if (pitch != null || yaw != null)
        {
            if (pitch != null) {
                location.setPitch(pitch);
            }
            if (yaw != null) {
                location.setYaw(yaw);
            }
        }
        else
        {
            Entity targetEntity = context.getTargetEntity();
            if (targetEntity == null)
            {
                return 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);
        }
    }
}

@@ -626,16 +626,76 @@
 oat yaw;
+%0A    private Float pitchOffset;%0A    private Float yawOffset;
 %0A%0A    @O
@@ -1110,32 +1110,377 @@
     yaw = null;%0A
+        %7Dif (parameters.contains(%22pitch_offset%22)) %7B%0A            pitchOffset = (float)parameters.getDouble(%22pitch_offset%22);%0A        %7D else %7B%0A            pitchOffset = null;%0A        %7D%0A        if (parameters.contains(%22yaw_offset%22)) %7B%0A            yawOffset = (float)parameters.getDouble(%22yaw_offset%22);%0A        %7D else %7B%0A            yawOffset = null;%0A
         %7D%0A    %7D%0A
@@ -2057,20 +2057,405 @@
         
-else
+if (pitchOffset != null %7C%7C yawOffset != null)%0A        %7B%0A            if (pitchOffset != null) %7B%0A                location.setPitch(location.getPitch() + pitchOffset);%0A            %7D%0A            if (yawOffset != null) %7B%0A                location.setYaw(location.getYaw() + yawOffset);%0A            %7D%0A        %7D%0A        if (pitchOffset == null && yawOffset == null && yaw == null && pitch == null)
 %0A       

Add pitch_offset and yaw_offset parameters to Orient action