using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
using IStalk.Network;

namespace IStalk.UI
{
    public class PauseMenu : MonoBehaviour
    {
        [SerializeField] private GameObject _pausePanel;
        [SerializeField] private Button _resumeBtn;
        [SerializeField] private Button _settingsBtn;
        [SerializeField] private Button _disconnectBtn;
        [SerializeField] private GameObject _settingsPanel;
        [SerializeField] private Slider _sensitivitySlider;
        [SerializeField] private Slider _masterVolumeSlider;
        [SerializeField] private Slider _musicVolumeSlider;
        [SerializeField] private Slider _sfxVolumeSlider;
        [SerializeField] private Toggle _invertYToggle;
        [SerializeField] private Button _settingsBackBtn;

        private bool _isPaused;

        private void Start()
        {
            _pausePanel.SetActive(false);
            if (_settingsPanel != null) _settingsPanel.SetActive(false);

            _resumeBtn.onClick.AddListener(Resume);
            _disconnectBtn.onClick.AddListener(Disconnect);

            if (_settingsBtn != null)
                _settingsBtn.onClick.AddListener(OpenSettings);
            if (_settingsBackBtn != null)
                _settingsBackBtn.onClick.AddListener(CloseSettings);

            LoadSettings();
        }

        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                if (_isPaused) Resume();
                else Pause();
            }
        }

        private void Pause()
        {
            _isPaused = true;
            _pausePanel.SetActive(true);
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
        }

        private void Resume()
        {
            _isPaused = false;
            _pausePanel.SetActive(false);
            if (_settingsPanel != null) _settingsPanel.SetActive(false);
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
        }

        private void OpenSettings()
        {
            if (_settingsPanel != null)
                _settingsPanel.SetActive(true);
        }

        private void CloseSettings()
        {
            if (_settingsPanel != null)
                _settingsPanel.SetActive(false);
            SaveSettings();
        }

        private void Disconnect()
        {
            RelayClient.Instance?.Disconnect();
            GameStartData.Clear();
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
            SceneManager.LoadScene("LobbyScene");
        }

        private void LoadSettings()
        {
            if (_sensitivitySlider != null)
                _sensitivitySlider.value = PlayerPrefs.GetFloat("Sensitivity", 2f);
            if (_masterVolumeSlider != null)
                _masterVolumeSlider.value = PlayerPrefs.GetFloat("MasterVolume", 1f);
            if (_musicVolumeSlider != null)
                _musicVolumeSlider.value = PlayerPrefs.GetFloat("MusicVolume", 0.5f);
            if (_sfxVolumeSlider != null)
                _sfxVolumeSlider.value = PlayerPrefs.GetFloat("SfxVolume", 0.8f);
            if (_invertYToggle != null)
                _invertYToggle.isOn = PlayerPrefs.GetInt("InvertY", 0) == 1;
        }

        private void SaveSettings()
        {
            if (_sensitivitySlider != null)
                PlayerPrefs.SetFloat("Sensitivity", _sensitivitySlider.value);
            if (_masterVolumeSlider != null)
                PlayerPrefs.SetFloat("MasterVolume", _masterVolumeSlider.value);
            if (_musicVolumeSlider != null)
                PlayerPrefs.SetFloat("MusicVolume", _musicVolumeSlider.value);
            if (_sfxVolumeSlider != null)
                PlayerPrefs.SetFloat("SfxVolume", _sfxVolumeSlider.value);
            if (_invertYToggle != null)
                PlayerPrefs.SetInt("InvertY", _invertYToggle.isOn ? 1 : 0);
            PlayerPrefs.Save();
            AudioListener.volume = PlayerPrefs.GetFloat("MasterVolume", 1f);
        }
    }
}
