﻿using UnityEngine;

public class Spawner : MonoBehaviour
{
	[SerializeField] private float innerRadius = 80;
	[SerializeField] private float outerRadius = 180;
	[SerializeField] private float spawnTimer = 5;
	[SerializeField] private float spawnRate = 60;
	[SerializeField] private float spawnCountdown = 0;
	[SerializeField] private GameObject enemyPrefab;

	private void Update()
	{
		float deltaTime = Time.deltaTime;
		spawnTimer -= deltaTime / spawnRate;
		spawnCountdown -= deltaTime;

		if (spawnTimer < 0)
		{
			spawnTimer = 0;
		}

		if (spawnCountdown <= 0)
		{
			Spawn();
			spawnCountdown = spawnTimer;
		}
	}

	private void Spawn()
	{
		float distance = Random.Range(innerRadius, outerRadius);
		Vector2 direction2D = Random.insideUnitCircle.normalized;
		Vector3 direction = new Vector3(direction2D.x, 0, direction2D.y);
		Vector3 position = transform.position + direction * distance;

		Instantiate(enemyPrefab, position, Quaternion.identity);
	}

	private void OnDrawGizmos()
	{
		Gizmos.color = Color.red;
		Gizmos.DrawWireSphere(transform.position, innerRadius);
		Gizmos.DrawWireSphere(transform.position, outerRadius);
	}
}
