using UnityEngine;
using UnityEngine.Audio;

public class AudioMaster : MonoBehaviour {
    public GameState GameState;
    public Options Options;

    public AudioMixer Mixer;
    public AudioMixerSnapshot PlayingSnapshot;
    public AudioMixerSnapshot PausedSnapshot;
    public AudioMixerSnapshot GameoverSnapshot;
    public float TransitionDuration;

    void Update() {
        float[] Weights = {
            GameState.Current == State.Playing ? 1 : 0,
            GameState.Current == State.Paused ? 1 : 0,
            GameState.Current == State.GameOver ? 1 : 0,
        };
        AudioMixerSnapshot[] Snapshots = {
            PlayingSnapshot,
            PausedSnapshot,
            GameoverSnapshot,
        };
        Mixer.TransitionToSnapshots(Snapshots, Weights, TransitionDuration);

        Mixer.SetFloat("Master Volume", ConvertRatioToDecibel(Options.MasterVolume));
        Mixer.SetFloat("Campfire Volume", ConvertRatioToDecibel(Options.CampfireVolume));
        Mixer.SetFloat("Ambient Volume", ConvertRatioToDecibel(Options.AmbientVolume));
        Mixer.SetFloat("Footstep Volume", ConvertRatioToDecibel(Options.FootstepVolume));
        Mixer.SetFloat("Diary Volume", ConvertRatioToDecibel(Options.DiaryVolume));
        Mixer.SetFloat("Teleport Volume", ConvertRatioToDecibel(Options.TeleportVolume));
    }

    private float ConvertRatioToDecibel(float x) {
        return (Mathf.Pow(x, 0.16f) - 1) * 60f;
    }
}