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

public class GoalFleeWithEgg : 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;

        GridPoint2 dest = BestFlee();
        if (dest == GridPoint2.None)
            return;

        actions.Add(new ActionMove(actor, actor.Location, dest));
    }

    private GridPoint2 BestFlee()
    {
        var valids = PointyHexPoint.GetOrthogonalNeighbors(actor.Location)
            .Where(p => GameMap.Available(p));

        if (valids.Count() == 0)
            return GridPoint2.None;

        return valids.OrderByDescending(q => PointyHexPoint.HexNorm(q - CENTER)).FirstOrDefault();
    }


}
