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

public class HasteAction : BaseAction
{
    private Unit targetUnit;

    private void Start() {
        maxDistance = (Range)10;
    }
    private void Update() {
        if (!IsActive()) {
            return;
        }
        StopAction();
    }
    public override string GetActionName()
    {
        return "Haste";
    }

    public override EnemyAIAction GetEnemyAIAction(GridPosition gridPosition)
    {
        return new EnemyAIAction {
            gridPosition = gridPosition,
            actionValue = 0,
        };
    }

    public override CancellationToken TakeAction(in GridPosition targetGridPosition, out CancellationToken cancellationToken)
    {
        targetUnit = LevelGrid.Instance.GetUnitAtGridPosition(targetGridPosition);
        HasteBuff hasteBuff = new HasteBuff();
        targetUnit.AddBuff(4,hasteBuff);
        StartAction(out cancellationToken);
        return cancellationToken;    
    }

    protected override bool IsValidTargetPositionSpecific(in GridPosition unitGridPosition, in GridPosition testGridPosition) {
        return (LevelGrid.Instance.HasAnyUnitOnGridPosition(testGridPosition)
                && (LevelGrid.Instance.GetUnitAtGridPosition(testGridPosition).IsEnemy() == unit.IsEnemy())
                && (IsClearShot(unitGridPosition, testGridPosition))
                );
    }

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