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

public class ActionMove : TurnAction
{
    private GridPoint2 from;
    private GridPoint2 to;
    private Actor actor;
    private SmoothMovement movement;
    private float durationFactor;
    public GridPoint2 EndPos { get { return to;  } }

    public ActionMove(Actor a, GridPoint2 from, GridPoint2 to, float durationFactor = 1.0f)
    {
        //Debug.Log(a.name + " constructing move from " + from + " to " + to);
        actor = a;
        this.from = from;
        this.to = to;
        this.durationFactor = durationFactor;
        Priority = 1;
    }

    //override public Render GetRender()
    //{
    //    //string msg = actor.name + " finished render of move from " + from + " to " + to;
    //    Render r = new SmoothMovement(actor.transform, GameMap.Cell[from].transform.position, GameMap.Cell[to].transform.position, durationFactor*TurnBasedGameLoop.instance.MovementRenderTime);
    //    //r.AddTrigger(new RenderTrigger(() => CombatLib.RotateToFace(actor.transform, GameMap.Cell[to].transform.position), 0f));
    //    r.AddTrigger(new RenderTrigger(() => actor.SendMessage("Animate", "Move", SendMessageOptions.DontRequireReceiver), 0.02f));
    //    if(actor.name.Contains("Player"))
    //        r.AddTrigger(new RenderTrigger(() => RenderSounds.PlaySound("PlayerFootstep"), 0));
    //    r.AddTrigger(new RenderTrigger(() => actor.SendMessage("Animate", "Idle", SendMessageOptions.DontRequireReceiver), 0.98f));
    //    //r.AddTrigger(new RenderTrigger(() => CombatLib.RotateToFace(actor.transform, CombatLib.Player.transform.position), 1f));

    //    //r.AddTrigger(new RenderTrigger(() => { Debug.Log(msg); }, 1f));
    //    return r; 
    //}

    override public void Perform()
    {
        //if (UnavailableCell())
        //{
        //    Result = ActionResult.Abort;
        //    return;
        //}
        AddRender();
        MoveActor();
        Result = ActionResult.SuccessOnlyAffectedSelf;        
    }

    private void AddRender()
    {
        Render r = new SmoothMovement(actor.transform, GameMap.Cell[from].transform.position, GameMap.Cell[to].transform.position, durationFactor * TurnBasedGameLoop.instance.MovementRenderTime);
        r.AddTrigger(new RenderTrigger(() => actor.SendMessage("Animate", "Move", SendMessageOptions.DontRequireReceiver), 0.02f));
        if (actor.name.Contains("Player"))
            r.AddTrigger(new RenderTrigger(() => RenderSounds.PlaySound("PlayerFootstep"), 0));
        r.AddTrigger(new RenderTrigger(() => actor.SendMessage("Animate", "Idle", SendMessageOptions.DontRequireReceiver), 0.98f));
        RenderPipeline.Render(r);
    }

    private bool UnavailableCell()
    {
        return ( !GameMap.Contains(to) || GameMap.Cell[to].HasActor() );
    }

    private void MoveActor()
    {
        //Debug.Log(actor.name + " performing move from " + from + " to " + to);
        if (GameMap.Cell[from].Actor == actor)
            GameMap.Cell[from].RemoveActor();
        GameMap.Cell[to].SetActor(actor);

        actor.Location = to;
        //actor.transform.position = GameMap.Cell[actor.Location].transform.position;
    }

}
