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

/// <summary>
/// Class for dealing with the spawning of Asteroids
/// </summary>
public class AsteroidSpawning : ObjectSpawning
{
    public GameObject asteroidPrefab;

    public float minWidthScaleRange;
    public float minHeightScaleRange;
    public float maxWidthScaleRange;
    public float maxHeightScaleRange;
    public int numberToSpawn;

    private void Awake()
    {
        _spawnedObject = asteroidPrefab;
        _numberToSpawn = numberToSpawn;
    }

    /// <summary>
    /// Reduce the spawned counter for this spawner
    /// </summary>
    public override void ReduceSpawnedCount()
    {
        objectCount = Mathf.Clamp(objectCount - 1, 0, _numberToSpawn);
        base.ReduceSpawnedCount();
    }

    /// <summary>
    /// Spawn the gameObject assigned to this spawner
    /// </summary>
    protected override void DoASpawn()
    {
        float xPos = Random.Range(-xSpawnRange, xSpawnRange);
        float yPos = Random.Range(-ySpawnRange, ySpawnRange);
        float zPos = Random.Range(-zSpawnRange, zSpawnRange);

        float widthScale = Random.Range(minWidthScaleRange, maxWidthScaleRange);
        float heightScale = Random.Range(minHeightScaleRange, maxHeightScaleRange);
        // If debug is checked then try to spawn the object above the player, this is to test collisions
        if (debugSpawn)
        {
            GameObject currentPlayer = GameObject.Find("Player");
            xPos = currentPlayer.transform.position.x;
            zPos = currentPlayer.transform.position.z;
            yPos = currentPlayer.transform.position.y + 10;
        }

        GameObject newSpawn = Instantiate(_spawnedObject);
        // Set the start height of the gameObject from the blackhole        
        HoverMovement newSpawnMovement = newSpawn.GetComponent<HoverMovement>();
        if (newSpawnMovement)
        {
            newSpawnMovement.startPositionVector = new Vector3(xPos, yPos, zPos);
            newSpawnMovement.ForceHeightAdjust(Random.Range(minSpawnHeight, maxSpawnHeight));
        }
        newSpawn.transform.localScale = new Vector3(widthScale, heightScale, widthScale);
        // Set the spawned to this on the gameObject
        AsteroidObjectShared newSpawnShared = newSpawn.GetComponent<AsteroidObjectShared>();
        if (newSpawnShared)
        {
            newSpawnShared.Spawner = this;
        }

        newSpawn.SetActive(true);

        objectCount++;
        base.DoASpawn();
    }
}
