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

public class AudioManager : MonoBehaviour {
    public List<Clip> audioClips;
    AudioSource audio;

	// Use this for initialization
	void Start () {
        LoadClips();
        audio = GetComponent<AudioSource>();
        audio.loop = false;
	}

    public void PlayClip(string clipName)
    {
        foreach(Clip c in audioClips)
        {
            if (c.Name == clipName)
                PlayAudioClip(c.MyClip, Random.Range(0.2f, c.Volume));
        }
    }

    void PlayAudioClip(AudioClip clip, float volume = 1f)
    {
        audio.clip = clip;
        audio.volume = volume;
        audio.pitch = Random.Range(0.85f, 1);
        audio.Play();
    }

    void LoadClips()
    {
        audioClips = new List<Clip>();

        // Clip 1
        Clip c1 = new Clip();
        c1.Name = "Glass Ping";
        c1.Volume = 1f;
        c1.MyClip = (Resources.Load("glassPing") as AudioClip);
        audioClips.Add(c1);

        // Clip 2
        Clip c2 = new Clip();
        c2.Name = "Swoop";
        c2.Volume = 1f;
        c2.MyClip = (Resources.Load("swoop") as AudioClip);
        audioClips.Add(c2);

        // Clip 3
        Clip c3 = new Clip();
        c3.Name = "Jar Glass Break";
        c3.Volume = 1f;
        c3.MyClip = (Resources.Load("jarGlassBreak") as AudioClip);
        audioClips.Add(c3);

        // Clip 4

    }
}

public struct Clip
{
    public string Name;
    public AudioClip MyClip;
    public float Volume;
}
