using UnityEngine;
using System.Collections;

public class BGMAudio : MonoBehaviour {

    public static BGMAudio bgma;
    bool noite;

    void Awake() {
        if (!bgma) {
            bgma = this;
            DontDestroyOnLoad(gameObject);
        } else {
            Destroy(gameObject);
        }
    }

    // Use this for initialization
    void Start () {
        noite = System.DateTime.Now.Hour <= 6 || System.DateTime.Now.Hour >= 20;
        ChangeAudio();
    }
	
	// Update is called once per frame
	void Update () {
        if (noite && (System.DateTime.Now.Hour > 6 && System.DateTime.Now.Hour < 20)) {
            noite = false;
            ChangeAudio();
        } else if(!noite && (System.DateTime.Now.Hour <= 6 || System.DateTime.Now.Hour >= 20)){
            noite = true;
            ChangeAudio();
        }
	}

    void ChangeAudio() {
        foreach (Transform t in transform) {
            AudioSource audioT = t.gameObject.GetComponentInChildren<AudioSource>();
            if (audioT) {
                Destroy(t.gameObject);
            }
        }
        GameObject audioNovo;
        if (noite) {
            audioNovo = (GameObject)Instantiate(Resources.Load("BGMNoite"), transform.position, transform.rotation);
        } else {
            audioNovo = (GameObject)Instantiate(Resources.Load("BGMDia"), transform.position, transform.rotation);
        }
        audioNovo.transform.SetParent(transform);
    }
}
