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

public class AudioController : MonoBehaviour {

    public AudioSource source;
    public AudioClip[] voiceovers;
    public float[] secondsPerVoiceover;

    int voiceOverIndex;

    public void Awake()
    {
        //World.onWorldChange += OnWorldChange;
    }

    public void Update()
    {
        var secondsLeft = AC.instance.secondsUntilAnswer;
        if (voiceOverIndex < secondsPerVoiceover.Length && secondsLeft < secondsPerVoiceover[voiceOverIndex])
        {
            var clip = voiceovers[voiceOverIndex++];
            if (source.isPlaying)
                source.Stop();
            source.clip = clip;
            source.Play();

        }
    }

    public void OnWorldChange(int prev, int current)
    {
        if (current >= 0 && current < voiceovers.Length && voiceovers[current] != null)
        {
            var clip = voiceovers[current];
            if (source.clip == clip)
                return;
            if (source.isPlaying)
                source.Stop();
            source.clip = voiceovers[current];
            source.Play();
        }
    }
}
