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

public class AudioManager : MonoBehaviour
{
    public AudioMixer masterMixer;
    public Slider sliderMusic, sliderEffects;

    [SerializeField] GameObject MainPanel = null;


    void Start()
    {
        InitSettings();

        if (MainPanel != null)
        {
            this.gameObject.SetActive(false);
        }

    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape) && MainPanel != null)
        {
            DisableAudioPanel();
        }
    }

    private void InitSettings()
    {
        sliderMusic.value = PlayerPrefs.GetFloat("MusicVolume", 80f);
        sliderEffects.value = PlayerPrefs.GetFloat("EffectsVolume", 80f);
        SetEffectsVolume(PlayerPrefs.GetFloat("EffectsVolume", 80f));
        SetMusicVolume(PlayerPrefs.GetFloat("MusicVolume", 80f));
    }

    public void SetEffectsVolume(float percentage)
    {
        float db = 20 * Mathf.Log10(percentage / 100);
        //Debug.Log("Efectos");
        masterMixer.SetFloat("EffectsParam", db);
        PlayerPrefs.SetFloat("EffectsVolume", percentage);
        PlayerPrefs.Save();
    }

    public void SetMusicVolume(float percentage)
    {
        float db = 20 * Mathf.Log10(percentage / 100);
        //Debug.Log("Music");
        masterMixer.SetFloat("MusicParam", db);
        PlayerPrefs.SetFloat("MusicVolume", percentage);
        PlayerPrefs.Save();
    }

    public void DisableAudioPanel()
    {
        this.gameObject.SetActive(false);
        MainPanel.SetActive(true);
    }

}
