using UnityEngine;

[RequireComponent(typeof(AudioSource))]
public class RandomContainer : MonoBehaviour
{
    [SerializeField] private float m_minNextPlay;
    [SerializeField] private float m_maxNextPlay;
    [SerializeField] private AudioClip[] m_audioClips;
    [SerializeField] private AudioSource m_emitter;
    [SerializeField] private bool m_playOnce;

    private float m_cooldown;

#region Unity Callback
    // Use this for initialization
    protected void Start ()
    {
        UpdateNextPlayCooldown();
    }
	
	// Update is called once per frame
	protected void Update ()
	{
	    m_cooldown -= Time.deltaTime;
	    if (m_cooldown <= 0f)
	    {
            m_emitter.PlayOneShot(m_audioClips[Random.Range(0, m_audioClips.Length)]);
	        UpdateNextPlayCooldown();

	        if (m_playOnce)
	        {
	            enabled = false;
	        }
	    }
	}

    private void UpdateNextPlayCooldown()
    {
        m_cooldown = Random.Range(m_minNextPlay, m_maxNextPlay);
    }
#endregion
}
