using UnityEngine;
using System.Collections;
using Gamelogic.Grids2;

public class ActionRoll : TurnAction
{

    private GridPoint2 direction;
    private Actor actor;

    public ActionRoll(Actor actor, GridPoint2 direction)
    {
        this.actor = actor;
        this.direction = direction;
    }

    override public void Perform()
    {
        GridPoint2 land = GetLanding();
        //TurnAction movePlayer = GetPlayerMove(land);

        int dist = PointyHexPoint.HexNorm(land - actor.Location);
        SmoothMovement r = new SmoothMovement(actor.transform, actor.transform.position, GameMap.Cell[land].transform.position, dist * 0.12f);

        RenderPipeline.RenderCode(() => actor.SendMessage("Animate", "Roll"));
        RenderPipeline.Render(r);
        RenderPipeline.RenderCode(() => actor.SendMessage("Animate", "Idle"));

        MoveActor(land);
    }

    private void MoveActor(GridPoint2 to)
    {
        if (GameMap.Cell[actor.Location].Actor == actor)
            GameMap.Cell[actor.Location].RemoveActor();
        GameMap.Cell[to].SetActor(actor);
        actor.Location = to;
    }

    public GridPoint2 GetLanding()
    {
        GridPoint2 check = actor.Location;

        do
        {
            check = check + direction;
            if(!GameMap.Available(check))
                return check - direction;

        } while (GameMap.Contains(check));

        Debug.LogError("ActionSlam.GetLanding ran past the end of the map, this is a bug.  Best-guess " + (check - direction));
        return check - direction;
    }
}
