using System.Collections;
using System.Collections.Generic;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
using UnityEngine;

public class RoundsManager : MonoBehaviour
{
    public static RoundsManager instance;

    Round[] rounds;
    public Transform[] SpawnPoints;
    [SerializeField] float TimeBetweenRounds = 5.0f;
    [SerializeField] bool musicBetweenRounds = true;
    //AudioSource[] limitSounds;
    int currentRound = 0;

    private FMOD.Studio.EventInstance instancefmod;
    public FMODUnity.EventReference musicEvent;

    public UnityEvent OnRoundFinished;
    public UnityEvent AllRoundsFinished;

    void Awake()
    {
        instance = this;
        rounds = GetComponentsInChildren<Round>();
        //limitSounds = GetComponents<AudioSource>();
    }

    private void Start()
    {
        Invoke("ActivateNextRound", TimeBetweenRounds);
        FMODUnity.RuntimeManager.GetBus("bus:/").stopAllEvents(FMOD.Studio.STOP_MODE.IMMEDIATE);
        
        instancefmod = FMODUnity.RuntimeManager.CreateInstance(musicEvent);
        instancefmod.start();
        instancefmod.release();
        if (musicBetweenRounds) 
        { 
            instancefmod.setParameterByName("UnmuteTrack2", 0);
            instancefmod.setParameterByName("UnmuteTrack3", 0);
        }
        

        OnRoundFinished.Invoke();
    }

    void OnDisable()
    {
        instancefmod.stop(FMOD.Studio.STOP_MODE.IMMEDIATE);
    }

    void Update()
    {
        if ((currentRound >= 0) && (currentRound < rounds.Length))
        {
            if (rounds[currentRound].RoundHasEnded())
            {
                OnRoundFinished.Invoke();
                if (musicBetweenRounds) { instancefmod.setParameterByNameWithLabel("Ronda", "Reposo"); }
                currentRound++;

                if (currentRound < rounds.Length) { Invoke("ActivateNextRound", TimeBetweenRounds); }
                /*else
                {
                    limitSounds[1].Play();
                }*/
            }
        }
        else if(currentRound >= rounds.Length)
        {
            AllRoundsFinished.Invoke();
            Invoke("LoadNextLevel", 5.0f);
        }
    }

    private void ActivateNextRound()
    {
        rounds[currentRound].StartRound();
        if (musicBetweenRounds)
        {
            instancefmod.setParameterByNameWithLabel("Ronda", "EnRonda");

            if (currentRound >= 2)
                instancefmod.setParameterByName("UnmuteTrack3", 1);
            else if (currentRound >= 1)
                instancefmod.setParameterByName("UnmuteTrack2", 1);
        }      
    }

    void LoadNextLevel()
    {
        if(PlayerPrefs.GetInt("LastLevelUnlocked", 1) < SceneManager.sceneCountInBuildSettings)
        {
            int currentScene = SceneManager.GetActiveScene().buildIndex;
            PlayerPrefs.SetInt("LastLevelUnlocked", currentScene + 1);
            SceneManager.LoadScene(currentScene + 1);
        }
        else
        {
            Debug.Log("JUEGO FINALIZADO");
        }
    }
}
