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

public class SoundManager : MonoBehaviour
{
    public AudioSource efxSource;                   //Drag a reference to the audio source which will play the sound effects.
    public AudioSource musicSource;                 //Drag a reference to the audio source which will play the music.
    public static SoundManager instance = null;     //Allows other scripts to call functions from SoundManager.             
    public float lowPitchRange = 1f;              //The lowest a sound effect will be randomly pitched.
    public float highPitchRange = 1f;            //The highest a sound effect will be randomly pitched.

    public AudioClip centerhex;
    public AudioClip innerhex;
    public AudioClip outerhex;
    public AudioClip innerbumper;
    public AudioClip outerbumper;
    public AudioClip wall;
    public AudioClip pulse;

    public List<AudioSource> audioSources;
    private int audioId = 0;

    void Awake()
    {
        //Check if there is already an instance of SoundManager
        if (instance == null)
            //if not, set it to this.
            instance = this;
        //If instance already exists:
        else if (instance != this)
            //Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.
            Destroy(gameObject);
        
        //Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene.
        DontDestroyOnLoad(gameObject);
        
        for( int i = 0; i < audioSources.Count; i++)
        {
            audioSources[i] = this.gameObject.AddComponent<AudioSource>();
        }
    }

    //Used to play single sound clips.
    //public void PlaySingle(AudioClip clip)
    public void PlaySingleByTag(string tag)
    {
        AudioClip clip = null;

        //Differentiate by tag
        switch( tag )
        {
            case "centerhex":
                clip = centerhex;
                break;
            case "innerhex":
                clip = innerhex;
                break;
            case "outerhex":
                clip = outerhex;
                break;
            case "innerbump":
                clip = innerbumper;
                break;
            case "outerbump":
                clip = outerbumper;
                break;
            case "wall":
                clip = wall;
                break;
            case "pulse":
                clip = pulse;
                break;
        }

        if (clip == null)
        {
            print("audioclip not found with tag " + tag);
            return;
        }

        if (audioId == audioSources.Count - 1)
            audioId = 0;
        else
            audioId++;

        efxSource = audioSources[audioId];

        //Set the clip of our efxSource audio source to the clip passed in as a parameter.
        efxSource.clip = clip;

        //Vary pitch
        float randomPitch = Random.Range(lowPitchRange, highPitchRange);
        efxSource.pitch = randomPitch;

        //Play the clip.
        efxSource.Play();
    }

    //RandomizeSfx chooses randomly between various audio clips and slightly changes their pitch.
    /*public void RandomizeSfx(params AudioClip[] clips)
    {
        //Generate a random number between 0 and the length of our array of clips passed in.
        int randomIndex = Random.Range(0, clips.Length);

        //Choose a random pitch to play back our clip at between our high and low pitch ranges.
        float randomPitch = Random.Range(lowPitchRange, highPitchRange);

        //Set the pitch of the audio source to the randomly chosen pitch.
        efxSource.pitch = randomPitch;

        //Set the clip to the clip at our randomly chosen index.
        efxSource.clip = clips[randomIndex];

        //Play the clip.
        efxSource.Play();
    }//*/
}