using System;
using System.Collections.Generic;
using Grid;

namespace Actions {
    public class InteractAction : BaseAction {
        private int MaxInteractDistance { get; set; }

        protected override void Awake() {
            base.Awake();
            ActionPointsCost = 1;
            MaxInteractDistance = 1;
            ActionName = "Interact";
        }

        public override void TakeAction(GridPosition gridPosition, Action onActionComplete) {
            LevelGrid.Instance.GetInteractableAtGridPosition(gridPosition).Interact(OnInteractComplete);
            ActionStart(onActionComplete);
        }

        public override List<GridPosition> GetValidActionGridPositionList() {
            List<GridPosition> validGridPositionList = new();
            GridPosition unitGridPosition = Unit.GridPosition;
            for (int x = -MaxInteractDistance; x <= MaxInteractDistance; x++) {
                for (int z = -MaxInteractDistance; z <= MaxInteractDistance; z++) {
                    GridPosition offsetGridPosition = new(x, z);
                    GridPosition testGridPosition = unitGridPosition + offsetGridPosition;
                    if (!LevelGrid.Instance.IsValidGridPosition(testGridPosition)) continue; //Only return valid grid positions
                    if (LevelGrid.Instance.GetInteractableAtGridPosition(testGridPosition) is null) continue; //No Interactable on this GridPosition
                    validGridPositionList.Add(testGridPosition);
                }
            }

            return validGridPositionList;
        }

        protected override EnemyAIAction GetEnemyAIAction(GridPosition gridPosition) => new(gridPosition, 0);

        private void OnInteractComplete() => ActionComplete();
    }
}