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

/// <summary>
/// Class for dealing with the blackhole
/// </summary>
public class BlackHoleManager : MonoBehaviour
{
    // List of blackhole force scripts currently in play
    public static List<BlackHoleForce> blackHoleForceObjects = new List<BlackHoleForce>();

    // the GO of the blackhole
    public GameObject blackHoleModel;

    // Bools to lock the size and to time grow it
    public bool sizeLocked;
    public bool timedGrow;

    // Vars for the blackhole size
    public float initialBlackHoleRadius;
    public float blackHoleSizeIncrement;
    public float blackHoleSizeTime;
    public float gravityPullFoce;

    public float currentBlackHoleRadius;

    private float _currentBlackHoleTimePassed;

    // Vars for dealing with the blackhole SFX
    private AudioSource _currentAudioSource;
    public AudioClip blackHoleAmbiantSFX;
    public int maxAudioSize = 70;
    public bool playBlackHoleAmbiantNoise;

    /// <summary>
    /// Method to get the current size of the blackhole
    /// </summary>
    /// <returns>Size of the blackhole</returns>
    public float GetCurrentBlackHoleSize()
    {
        return currentBlackHoleRadius;
    }

    // Start is called before the first frame update
    void Awake()
    {
        // set the initial blackhole vars and size
        _currentBlackHoleTimePassed = 0.0f;
        blackHoleModel.transform.localScale = Vector3.one * initialBlackHoleRadius;
        currentBlackHoleRadius = initialBlackHoleRadius;

        // Set the audio source and SFX
        _currentAudioSource = gameObject.GetComponent<AudioSource>();
        if (_currentAudioSource)
        {
            if (playBlackHoleAmbiantNoise)
            {
                _currentAudioSource.clip = blackHoleAmbiantSFX;
                _currentAudioSource.loop = true;
                _currentAudioSource.Play();
            }
            else
            {
                _currentAudioSource.clip = null;
                _currentAudioSource.Stop();
            }
        }
    }


    private void Update()
    {
        // fort nite


        BlackHoleAudio();
        if (timedGrow)
        {
            TimedGrow();
        }               
    }

    /// <summary>
    /// Method for dealing with increasing the sound catchment area when the blackhole grows
    /// </summary>
    public void BlackHoleAudio()
    {
        GetComponent<AudioSource>().maxDistance = currentBlackHoleRadius + maxAudioSize;
    }

    /// <summary>
    /// Method for dealing with a timed blackhole grow, if the size is not locked and the timed option is checked, a given amount of mass is given to the blackhole every X seconds
    /// </summary>
    private void TimedGrow()
    {
        if (!sizeLocked)
        {
            if (_currentBlackHoleTimePassed >= blackHoleSizeTime)
            {
                blackHoleModel.transform.localScale += Vector3.one * blackHoleSizeIncrement;
                currentBlackHoleRadius += blackHoleSizeIncrement;
                _currentBlackHoleTimePassed = 0.0f;
            }
            else
            {
                _currentBlackHoleTimePassed += Time.deltaTime;
            }
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        // If we collide with an object trigger is blackhole hit method
        BlackHoleForce bhfHit = collision.gameObject.GetComponent<BlackHoleForce>();
        if (bhfHit)
        {
            bhfHit.HitBlackHole();
        }
    }

    private void FixedUpdate()
    {
        // Apply force to every blackhole force object in the list
        foreach (BlackHoleForce bhfObject in blackHoleForceObjects)
        {
            bhfObject.ApplyBlackHoleForce(gravityPullFoce * Time.fixedDeltaTime);
        }
    }

    /// <summary>
    /// Method for dealing with asteroids growing the blackhole since they increase it every X hits
    /// </summary>
    public void AsteroidGrow()
    {
        return;
    }

    /// <summary>
    /// Method for growing the blackhole if the size has not been locked
    /// </summary>
    public void Grow()
    {
        if (!sizeLocked)
        {
            blackHoleModel.transform.localScale += Vector3.one * blackHoleSizeIncrement;
            currentBlackHoleRadius += blackHoleSizeIncrement;
        }
    }
}
