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

/// <summary>
/// Base class for handling gameObject spawning, this should be inherited by the specific spawners we need
/// </summary>
public class ObjectSpawning : MonoBehaviour
{
    // The gameObject to spawn from the spawner
    protected GameObject _spawnedObject;

    // If true the spawner will spawn new objects when it can
    public bool canSpawn;

    // The Spawn ranges, a random number between - and + values will be selected
    public float xSpawnRange;
    public float ySpawnRange;
    public float zSpawnRange;

    public int objectCount;
    protected int _numberToSpawn;

    // The heights the object is spawned from the blackhole
    public float minSpawnHeight;
    public float maxSpawnHeight;

    public float boundaryMargin;

    // The interval the spawner is ran/checked at
    public float spawnMaxIntervalTime;
    private float _spawnCurrentIntervalTime;

    public bool debugSpawn;

    public virtual void ReduceSpawnedCount()
    {        
    }

    private void Start()
    {
        _spawnCurrentIntervalTime = 2.0f;        
    }

    protected virtual void DoASpawn()
    {        
    }

    protected virtual void GetCurrentCount()
    {
    }

    private void Update()
    {
        GetCurrentCount();

        // Can this spawner curerntly spawn?
        if (canSpawn)
        {
            // Check if the wait for the spawning tick before spawning
            if (_spawnCurrentIntervalTime >= spawnMaxIntervalTime)
            {
                if (objectCount < _numberToSpawn)
                {
                    // The child scripts spawn script is run
                    DoASpawn();
                }

                // Restart the current spawn tick
                _spawnCurrentIntervalTime = 0.0f;
            }
            else
            {
                _spawnCurrentIntervalTime += Time.deltaTime;
            }
        }
    }
}
