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

public class PauseMenuManager : MonoBehaviour
{
    public GameObject pauseMenu;
    public Slider sensSlider;
    public Slider volumeSlider;
    public MouseLook mouse;
    public AudioMixer mixer;

    private bool _active = false;

    void Start(){
        sensSlider.value = PlayerPrefs.GetFloat("sens", 400);
        volumeSlider.value  = PlayerPrefs.GetFloat("volume", 0);
        ChangeVolume();
        ChangeSens();
        pauseMenu.SetActive(false);
    }
    void Update(){
        if(Input.GetKeyDown(KeyCode.Escape) && !_active){
            _active = true;
            pauseMenu.SetActive(true);
            Time.timeScale = 0;
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }
        else if(Input.GetKeyDown(KeyCode.Escape) && _active){
            _active = false;
            pauseMenu.SetActive(false);
            Time.timeScale = 1;
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }
    }

    public void Resume(){
        _active = false;
        pauseMenu.SetActive(false);
        Time.timeScale = 1;
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    public void Exit(){
        Time.timeScale = 1;
        GameObject.FindGameObjectWithTag("SceneSwitcher").GetComponent<SceneSwitcher>().SwitchScene("MainMenu");
    }

    public void ChangeSens(){
        PlayerPrefs.SetFloat("sens", sensSlider.value);
        mouse.sensitivity = sensSlider.value;
    }

    public void ChangeVolume(){
        PlayerPrefs.SetFloat("volume", volumeSlider.value);
        mixer.SetFloat("volume", volumeSlider.value);
    }
}
