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

public class SoundManager : MonoBehaviour
{
    public static SoundManager sndManager;
    public AudioSource backgroundMusicSource;
    public AudioSource effectSource;
    public AudioClip[] clips;
    public AudioClip[] heartbeats;
    public int currentClip = 0;

    private void Awake()
    {
        if (sndManager == null)
        {
            sndManager = this;
        }
        else if (sndManager != this) {
            Destroy(gameObject);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        backgroundMusicSource = GetComponent<AudioSource>();
        Invoke("PlayNext", clips[0].length);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    void PlayNext() {
        currentClip++;
        bool loop = true;
        if (currentClip > clips.Length -1)
        {
            currentClip = 0;
            loop = false;
        }
        changeClip(clips[currentClip], loop);
    }

    public void changeClip(AudioClip clip, bool loop) {
        backgroundMusicSource.Stop();
        backgroundMusicSource.clip = clip;
        backgroundMusicSource.Play();
        backgroundMusicSource.loop = loop;
        if (!loop) Invoke("PlayNext", clip.length);

    }

    public void changeHeartBeat(int speed) {
        effectSource.Stop();
        effectSource.clip = heartbeats[speed];
        effectSource.Play();
        effectSource.loop = true;
    }


    public void playLast() {
        backgroundMusicSource.Stop();
        backgroundMusicSource.clip = clips[clips.Length-1];
        backgroundMusicSource.Play();
    }
    
    
}
