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

public class SoundController : MonoBehaviour {

    public static SoundController instance;
    public AudioMixer mixer;

    public AudioClip[] audio;
    
    private AudioSource musicAudio;
    private AudioSource sfxAudio;

    public float sfxVolume;
    public float musicVolume;
    public float masterVolume;

    void Awake() {

        if (instance == null)
            instance = this;
        else if (instance != this)
        {
            Destroy(gameObject);
            return;
        }

        musicAudio = GetComponents<AudioSource>()[0];
        sfxAudio = GetComponents<AudioSource>()[1];


        //get music volume from mixer
        mixer.GetFloat("Music_Vol", out musicVolume);
        //remap from mixer range to 0 - 1
        musicVolume = musicVolume.Remap(-40, 0, 0,1);


        //get sfx volume
        mixer.GetFloat("SFX_Vol", out sfxVolume);
        //remap from mixer range to 0 - 1
        sfxVolume =  sfxVolume.Remap(-40, 0, 0, 1);


        //get master volume from mixer
        mixer.GetFloat("Master_Vol", out masterVolume);
        //remap from mixer range to 0 - 1
        masterVolume = masterVolume.Remap(-40, 0, 0, 1);

    }

    public void SetMusicVolume(float val) {
        musicVolume = val;
        mixer.SetFloat("Music_Vol", musicVolume.Remap(0,1,-20,0));
    }

    public void SetSFXVolume(float val)
    {
        sfxVolume = val;
        mixer.SetFloat("SFX_Vol", sfxVolume.Remap(0, 1, -20, 0));
    }

    public void SetMasterVolume(float val)
    {
        masterVolume = val;
        mixer.SetFloat("Master_Vol", masterVolume.Remap(0, 1, -20, 0));
    }

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

    public void PlayOneShot(string clipName) {

        //play clip with name (gets index from config
        sfxAudio.PlayOneShot(audio[Config.soundEffects[clipName]]);
    }
}
