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

public enum AudioClip
{
    CountdownBeep,
    TurbineRoar,
    TurbineSpin,
    SnowSlideSledge,
    Explosion,
    EnemyHit,
    ItemPickupEffect
}

[Serializable]
public struct AudioClipSource
{
    public AudioClip Clip;
    public AudioSource Source;
}

public class AudioManager : Manager<AudioManager>
{
    [SerializeField]
    private List<AudioClipSource> audioClipSources = new List<AudioClipSource>();
    [SerializeField]
    private float globalAudioVolume;
    
    private Dictionary<AudioClip, AudioSource> audioSources = new Dictionary<AudioClip, AudioSource>();

    public void SetGlobalAudioVolume(float volume)
    {
        globalAudioVolume = volume;

        AudioListener.volume = globalAudioVolume;
    }

    public void OnGameStateChanged(GameState newState, GameState oldState)
    {
        if(newState == GameState.Play && UIManagerMenu.Instance != null)
        {
            SetGlobalAudioVolume(PersistantStorage.Instance.AudioVolume);
        }
    }

    protected override void Awake()
    {
        base.Awake();

        foreach(AudioClipSource audioClipSource in audioClipSources)
            audioSources[audioClipSource.Clip] = audioClipSource.Source;
    }

    protected void Start()
    {
        GameStateManager.Instance.OnGameStateChangedEvent.AddListener(OnGameStateChanged);
    }

    protected void OnDestroy()
    {
        GameStateManager.Instance.OnGameStateChangedEvent.RemoveListener(OnGameStateChanged);
    }

    public AudioSource GetAudioSource(AudioClip audioClip)
    {
        AudioSource audioClipSource = audioSources[audioClip];
        return audioClipSource;
    }
}
