using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;

public class ProjectileTower : AimTower
{
    [SerializeField, Range(0.01f, 20f)] private float attackSecondsDelay = 1f;
    [SerializeField] private ProjectilePattern[] projectileSequence;

    private ProjectileSpawner projectileSpawner;

    protected override void Awake()
    {
        base.Awake();

        projectileSpawner = GetComponent<ProjectileSpawner>();
        Assert.IsNotNull(projectileSpawner);

    }

    public override void Placed()
    {
        base.Placed();

        StartCoroutine(AttackLoop());
    }


    private IEnumerator AttackLoop()
    {
        do {
            if (GameManager.Instance.CurrentNumEnemies == 0)
            {
                yield return new WaitForEndOfFrame();
                continue;
            }

            yield return new WaitForSeconds(attackSecondsDelay);
            Debug.DrawRay(transform.position, horizontalArc.ToKnobVector, Color.red, attackSecondsDelay);
            Debug.DrawRay(transform.position, verticalArc.ToKnobVector, Color.green, attackSecondsDelay);
            Debug.DrawRay(transform.position, AimDirection, Color.yellow, attackSecondsDelay);
            projectileSpawner.RunBulletSequence(barrel.Tip.position, transform.up, AimDirection, projectileSequence);
            this.OnAttack?.Invoke();
        } while (true);
    }
}