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

public class Escaper : MonoBehaviour 
{
    private GridPoint2 CENTER = new GridPoint2(5, 5);
    private Actor actor;
    void Start()
    {
        actor = GetComponent<Actor>();
    }

    public void PlanTurn(List<TurnAction> actions)
    {
        if (!actor.IsCarryingEgg)
            return;

        if (!GameMap.Cell[actor.Location].Contains("EdgeTile"))
            return;

        actions.Add(new CodeAction(() =>
            {
                PerformEscape();
                return ActionResult.SuccessOnlyAffectedSelf;
            }, 888));
    }

    private void PerformEscape()
    {
        if (actor != null && actor.IsCarryingEgg)
        {
            Vector3 cellPos = GameMap.Cell[actor.Location].transform.position;
            Vector3 escapeDir = (cellPos - GameMap.Cell[CENTER].transform.position).normalized;
            SmoothMovement escape = new SmoothMovement(actor.transform, transform.position, actor.transform.position + 1.3f * escapeDir, 0.7f, 0.3f);
            
            RenderPipeline.RenderSound("Escape");
            RenderPipeline.Render(escape);
            RenderPipeline.RenderCode(() => ScoreBoard.instance.EggsLeft--);
            RenderPipeline.RenderCode(() => GameObject.Destroy(actor.gameObject));

            GameMap.Cell[actor.Location].RemoveActor();
        }
    }


}
