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

public class GoalGrabEgg : MonoBehaviour 
{
    private Actor actor;
    void Start()
    {
        actor = GetComponent<Actor>();
    }

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

        GridPoint2 egg = AdjacentEgg();
        if (egg != GridPoint2.None)
        {
            Grab(actions, egg);
        }
    }

    private void Grab(List<TurnAction> actions, GridPoint2 egg)
    {
        actions.Add(new ActionGrabEgg(actor, (Actor)GameMap.Cell[egg].Actor));        
    }

    private GridPoint2 AdjacentEgg()
    {
        var adjEggs = PointyHexPoint.GetOrthogonalNeighbors(actor.Location)
            .Where(o => GameMap.Contains(o))
            .Where(p => GameMap.Cell[p].HasActor())
            .Where(q => GameMap.Cell[q].Actor.name == "Egg(Clone)");

        if (adjEggs.Count() == 0)
            return GridPoint2.None;
        else
            return adjEggs.FirstOrDefault();
    }
}
