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 TimerHelper _spawnCurrentTimer;

    public bool debugSpawn;

    public virtual void ReduceSpawnedCount()
    {        
    }

    private void Start()
    {
        _spawnCurrentTimer = new TimerHelper(spawnMaxIntervalTime);
    }

    protected virtual void DoASpawn()
    {        
    }

    protected virtual void GetCurrentCount()
    {
    }

    private void Update()
    {
        if (!GameEngine.isPaused)
        {
            GetCurrentCount();

            // Can this spawner curerntly spawn?
            if (canSpawn)
            {
                if (_spawnCurrentTimer.TimesRun == 0)
                {
                    _spawnCurrentTimer.HasTicked(spawnMaxIntervalTime - Time.deltaTime);
                }

                // Check if the wait for the spawning tick before spawning
                if (_spawnCurrentTimer.HasTicked(Time.deltaTime))
                {
                    if (objectCount < _numberToSpawn)
                    {
                        // The child scripts spawn script is run
                        DoASpawn();
                    }
                }
            }
        }
    }
}
