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

public class SoundManager : MonoBehaviour
{
    [SerializeField] private int audioPoolSize = 20;
    [SerializeField] private List<AudioClip> bumpSounds;
    [SerializeField] private List<AudioClip> dashSounds;
    [SerializeField] private List<AudioClip> impactSounds;
    [SerializeField] private AudioClip lastPlayerLose;
    [SerializeField] private AudioClip winScreenSound;
    [SerializeField] private AudioClip bomb;
    [SerializeField] private AudioClip intro1;
    [SerializeField] private AudioClip intro2;
    [SerializeField] private AudioClip arena;

    private AudioSource[] audioPool;

    public AudioSource introS;
    public AudioSource arenaS;

    public static SoundManager Instance { get; private set; }


    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
            audioPool = new AudioSource[audioPoolSize];
            for (int i = 0; i < audioPoolSize; i++)
            {
                audioPool[i] = gameObject.AddComponent<AudioSource>();
            }
        }
        else
        {
            Destroy(this.gameObject);
        }
    }


    public AudioSource Play(SoundType soundType)
    {
        switch (soundType)
        {
            case SoundType.Bump:
                return PlaySound(bumpSounds, 0.5f);
            case SoundType.Win:
                return PlaySound(winScreenSound, 1f);
            case SoundType.Lose:
                return PlaySound(lastPlayerLose, 1f);
            case SoundType.Bomb:
                return PlaySound(bomb, 1f);
            case SoundType.Impact:
                return PlaySound(impactSounds, 1f);
            case SoundType.Dash:
                return PlaySound(dashSounds, 1f);
            default:
                return null;
        }
    }


    private AudioSource PlaySound(AudioClip clip, float volume = 0.7f)
    {
        AudioSource source = GetReadyAudioSource();
        if (source != null)
        {
            source.clip = clip;
            source.volume = volume;
            source.Play();
            return source;
        }
        return null;
    }
    private AudioSource PlaySound(List<AudioClip> sounds, float volume = 0.7f)
    {
        return PlaySound(sounds[Random.Range(0, sounds.Count)], volume);
    }
    private AudioSource GetReadyAudioSource()
    {
        for (int i = 0; i < audioPoolSize; i++)
        {
            AudioSource source = audioPool[i];
            if (!source.isPlaying) return source;
        }
        Debug.LogError("Increase SoundManager pool size!");
        return null;
    }


    public enum SoundType
    {
        Bump,
        Win,
        Lose,
        Bomb,
        Impact,
        Dash
    }

    public IEnumerator PlayIntro()
    {
        Debug.Log("B");

        introS.volume = 1;
        introS.clip = intro1;
        introS.Play();
        yield return new WaitForSeconds(intro1.length);

        Debug.Log("A");
        introS.volume = 1;
        introS.clip = intro2;
        introS.loop = true;
        introS.Play();
    }

    public void StopIntro()
    {
        StopCoroutine("PlayIntro");
        introS.Stop();
    }

    public void PlayArena()
    {
        arenaS.volume = 0.6f;
        arenaS.clip = arena;
        arenaS.loop = true;
        arenaS.Play();
    }

    public void StopArena()
    {
        arenaS.Stop();
    }
}