using UnityEngine;
using System.Collections;

public class ActionGrabEgg : TurnAction
{
    private Actor egg;
    private Actor thief;

    public ActionGrabEgg(Actor actor, Actor egg)
    {
        this.egg = egg;
        this.thief = actor;
        Priority = 99;
    }

    override public void Perform()
    {
        if (egg == null)
            Debug.LogError("ERROR: ActionGrabEgg asked to grab a null egg");

        SmoothMovement moveGrab = new SmoothMovement(thief.transform, thief.transform.position, (thief.transform.position + egg.transform.position) * 0.5f, 0.05f);
        SmoothMovement moveBack = new SmoothMovement(thief.transform, (thief.transform.position + egg.transform.position) * 0.5f, thief.transform.position, 0.05f);

        RenderPipeline.Render(moveGrab);
        RenderPipeline.RenderSound("GrabEgg");
        RenderPipeline.RenderCode(() =>
            {
                egg.SendMessage("Animate", "Grabbed");
                egg.transform.parent = thief.transform;
                egg.transform.localPosition = new Vector2(0, 0.02f);                
            });
        RenderPipeline.Render(moveBack);

        GameMap.Cell[egg.Location].RemoveActor();
        egg.GetComponent<EggStatus>().IsStolen = true;
        thief.IsCarryingEgg = true;

        GameMap.Cell[thief.Location].SetActor(thief);  // to retrigger the Touch
        Result = ActionResult.SuccessOthersAffected;
    }

}
