using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Assets._game.scripts
{
    public class TestCamera : MonoBehaviour
    {

        public List<GameObject> TestPlayers;
        public float TimeBetweenSpawns = 3;

        public void Start()
        {
            StartCoroutine(SpawnRoutine());
        }

        private IEnumerator SpawnRoutine()
        {
            int spawned = 0;
            int max = TestPlayers.Count;
            while (spawned < max)
            {
                float endTime = Time.timeSinceLevelLoad + TimeBetweenSpawns;

                while (Time.timeSinceLevelLoad < endTime)
                {
                    yield return null;
                }
                var go = TestPlayers.ElementAt(spawned++);
                if (go)
                {
                    go.transform.position = new Vector3(Random.Range(-100, 100), 0, Random.Range(-100, 100));
                    CameraManager.Instance.AddTarget(go.transform);
                }
            }
            spawned = TestPlayers.Count - 1;

            while (spawned > 0)
            {
                float endTime = Time.timeSinceLevelLoad + TimeBetweenSpawns;

                while (Time.timeSinceLevelLoad < endTime)
                {
                    yield return null;
                }

                var go = TestPlayers.ElementAt(spawned--);
                CameraManager.Instance.RemoveTarget(go.transform);
            }
        }
    }
}
