using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class GrenadeAction : BaseAction
{
    public event EventHandler<onThrowEventArgs> onGrenadeThrow;
    public class onThrowEventArgs : EventArgs {
        public GridPosition targetGridPosition;
        public Unit shootingUnit;
        public Action grenadeExplodeAction;
    }
    private void Update() {
        if (!IsActive()) {
            return;
        }

    }
    private void Start() {
        actionCost = 1;
        maxDistance = (Range)7;
    }
    public override string GetActionName()
    {
        return "Grenade";
    }

    public override EnemyAIAction GetEnemyAIAction(GridPosition gridPosition)
    {
        return new EnemyAIAction {
            gridPosition = gridPosition,
            actionValue = 0,
        };
    }
    private void OnGrenadeHitComplete() {
        StopAction();
    }
    public override CancellationToken TakeAction(in GridPosition gridPosition, out CancellationToken cancellationToken)
    {
        onGrenadeThrow?.Invoke(this, new onThrowEventArgs{
            targetGridPosition = gridPosition,
            shootingUnit = unit,
            grenadeExplodeAction = OnGrenadeHitComplete,
        });

        return StartAction(out cancellationToken);  
    }

    protected override bool IsValidTargetPositionSpecific(in GridPosition unitGridPosition, in GridPosition testGridPosition) {
        return ((IsClearShot(unitGridPosition, testGridPosition)));
    }

    public bool IsClearShot(in GridPosition unitGridPosition, in GridPosition testGridPosition) {
        float unitShoulderHeight = 1.7f;
        return LevelGrid.Instance.IsLineOfSight(unitGridPosition, testGridPosition, unitShoulderHeight);
    }
}