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

public class ActionAttackSplat : TurnAction
{
    private GridPoint2 start;
    private Transform shotPrefab;
    private Transform hitPrefab;

    private List<Actor> splattedActors;

    public ActionAttackSplat(GridPoint2 start, Transform shotPrefab, Transform hitPrefab)
    {
        this.start = start;
        this.shotPrefab = shotPrefab;
        this.hitPrefab = hitPrefab;
    }

    override public void Perform()
    {
        foreach(GridPoint2 p in PointyHexPoint.GetOrthogonalNeighbors(start))
        {
            if (GameMap.Contains(p) && GameMap.Cell[p].HasActor())
                AttackCell(p);
        }

        if (splattedActors != null)
        {
            RenderPipeline.RenderCode(() => { return; }, 0.2f);
            RenderPipeline.RenderSound("Splat" + Mathf.Clamp(splattedActors.Count, 0, 6));
            splattedActors.ForEach(a => a.Die());
        }

        Result = ActionResult.SuccessOthersAffected;
    }

    private void AttackCell(GridPoint2 target)
    {
        
        Actor a = (Actor)GameMap.Cell[target].Actor;
        if (!a.IsSplatable)
            return;

        if (splattedActors == null)
            splattedActors = new List<Actor>();

        splattedActors.Add(a);

        RenderSplat r = new RenderSplat(start, target, shotPrefab, hitPrefab);
        RenderPipeline.Render(r);
    }
}
