using System;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.UI;

public class OptionsMenuUI : MonoBehaviour
{
    [Header("Audio")]
    [SerializeField] private AudioMixer mainAudioMixer;
    [SerializeField] private Slider masterVolumeSlider;
    [SerializeField] private Slider musicVolumeSlider;
    [SerializeField] private Slider VFXVolumeSlider;

    [Header("Graphics")]
    [SerializeField] private GameObject optionsUIObject;
    [SerializeField] private Toggle fullscreenToggle;
    [SerializeField] private Toggle resolutionToggle;
    [SerializeField] private Text fullscreenModeText;
    [SerializeField] private Text resolutionValueText;
    
    private string _fullscreenText = "Fullscreen";
    private string _windowedText = "Windowed";

    private string _highResText = "1920x1080";
    private string _lowResText = "640x360";
    
    // --- AUDIO SECTION ---
    private void Awake()
    {
        DeactivateChild();
    }

    public void DeactivateChild()
    {
        optionsUIObject.SetActive(false);
    }

    public void ActivateChild()
    {
        optionsUIObject.SetActive(true);
    }
    public void SetMasterVolume()
    {
        float db = Mathf.Log10(Mathf.Clamp(masterVolumeSlider.value, 0.0001f, 1f)) * 20f;
        mainAudioMixer.SetFloat("MasterVolume", db);
    }
    
    public void SetMusicVolume()
    {
        float db = Mathf.Log10(Mathf.Clamp(musicVolumeSlider.value, 0.0001f, 1f)) * 20f;
        mainAudioMixer.SetFloat("MusicVolume", db);
    }
    
    public void SetVFXVolume()
    {
        float db = Mathf.Log10(Mathf.Clamp(VFXVolumeSlider.value, 0.0001f, 1f)) * 20f;
        mainAudioMixer.SetFloat("VFXVolume", db);
    }

    // --- VIDEO SECTION ---
    public void SetResolution()
    {
        if (resolutionToggle.isOn)
        {
            Screen.SetResolution(1920, 1080, Screen.fullScreen);
            resolutionValueText.text = _highResText;
        }
        else
        {
            Screen.SetResolution(640, 360, Screen.fullScreen);
            resolutionValueText.text = _lowResText;
        }
    }
    
    public void SetFullscreen()
    {
        Screen.fullScreen = fullscreenToggle.isOn;
        if (fullscreenToggle.isOn)
        {
            fullscreenModeText.text = _fullscreenText;
        }
        else
        {
            fullscreenModeText.text = _windowedText;
        }
    }
}
