using UnityEngine;
using System.Collections;
using System;

public class SoundManager : MonoBehaviour {

    public enum Phase
    {
        None,
        Intro,
        Scene,
        EndingInfiltratorWins,
        EndingSectWins
    }

    public enum Sfx
    {
        sfx_mormorio_adepti,
        sfx_fruscio_tonache_adepti,
        sfx_alzata_fiaccola,
        sfx_inginocchiamento,
        sfx_spostamento_colore,
        sfx_spostamento_quadrato,
        sfx_spostamento_libero,
        sfx_ordine_alzata_fiaccola,
        sfx_ordine_inginocchiamento,
        sfx_accusa_officiante,
        sfx_selzione_adepto,
        sfx_demone,
        sfx_compimento_rito,
        sfx_error,
        sfx_fallimento_sabotaggio,
        amb_fiaccole,
        amb_pioggia,
        amb_tuono
    }

    private Phase phase = Phase.None;
    private AudioSource[] audioSources;
    private const string sfxSuffix = " (1)";

    private static SoundManager instance;

    // Use this for initialization
    void Start () {
        audioSources = GetComponentsInChildren<AudioSource>(true);        
        instance = this;
        SetPhase(Phase.Intro);
    }

    // Update is called once per frame
    void Update () {
	
        if (Input.GetKeyDown(KeyCode.I)) // DEBUG
        {
            SetPhase(Phase.Scene);
        }
	}

    public static void ChangePhase(Phase phase)
    {
        if (instance != null)
        {
            instance.SetPhase(phase);
        }
    }

    public static void PlaySfx(Sfx sfx)
    {
        if (instance != null)
        {
            instance.PlaySfx(sfx.ToString() + sfxSuffix);
        }
    }

    private void SetPhase(Phase phase)
    {
        if (this.phase != phase)
        {
            this.phase = phase;
            switch (phase)
            {
                case Phase.None:
                    PlayMusic("");
                    break;
                case Phase.Intro:
                    PlayMusic("Intro Music");
                    break;
                case Phase.Scene:
                    PlayMusic("Scene Music");
                    break;
                case Phase.EndingInfiltratorWins:
                    PlayMusic("Outro Music Good");
                    break;
                case Phase.EndingSectWins:
                    PlayMusic("Outro Music Evil");
                    break;
            }
        }
    }

    private void PlaySfx(string sfxName)
    {
        Debug.Log("Play SFX " + sfxName);
        foreach (AudioSource audioSource in audioSources)
        {
            if (audioSource.gameObject.name == sfxName)
            {
                audioSource.enabled = true;
            }
        }
    }

    private void PlayMusic(string audioName)
    {
        Debug.Log("Play Music " + audioName);
        foreach (AudioSource audioSource in audioSources)
        {
            if (audioSource.gameObject.name == audioName)
            {
                audioSource.enabled = true;
                //audioSource.Play();
            }
            else if (audioSource.isPlaying)
            {
                audioSource.enabled = false;
                //audioSource.Stop();
            }
        }
    }
}
