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

public class MusicPlayer : MonoBehaviour
{
    public AudioSource dayIntro;
    public AudioSource dayLoop;
    public AudioSource nightLoop;
    private float dayIntroDuration;

    void Awake()
    {
        EventManager.SubscribeSunrise(PlayDayMusic);
        EventManager.SubscribeSunset(PlayNightMusic);
        dayIntroDuration = dayIntro.clip.length;
        PlayNightMusic(); //TODO: This should be triggered by some event that happens when the game is started
    }

    void PlayDayMusic()
    {
    	nightLoop.Stop();
    	dayIntro.Play();
    	StartCoroutine(HandleDayMusic());
    }

    void PlayNightMusic()
    {
    	dayLoop.Stop();
    	nightLoop.Play();
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    IEnumerator HandleDayMusic()
    {
    	dayIntro.Play();
    	yield return new WaitForSeconds(dayIntroDuration);
    	dayLoop.Play();

    }

    // Update is called once per frame
    void Update()
    {
        //Short term hack because Roz in in the zone
        if (!dayIntro.isPlaying && !dayLoop.isPlaying && !nightLoop.isPlaying)
        {
            nightLoop.Play();
        }
    }
}
