using System;
using UniRx;
using UnityEngine;
using UnityEngine.UI;

namespace MyGame.Scenes.Settings.Scripts
{
    public class SettingDialog : MonoBehaviour
    {
        private static readonly int OpenTrigger = Animator.StringToHash("OpenTrigger");
        private static readonly int CloseTrigger = Animator.StringToHash("CloseTrigger");
        [SerializeField] private Animator backgroundAnimator;
        [SerializeField] private Button closeButton;
        
        [SerializeField] private Slider bgmVolumeSlider;
        [SerializeField] private Slider seVolumeSlider;
        [SerializeField] private Toggle vibrationalToggle;
        [SerializeField] private GameObject bgmSoundOnIcon;
        [SerializeField] private GameObject seSoundOnIcon;
        [SerializeField] private GameObject vibeOnIcon;
        [SerializeField] private Button howToPlayButton;

        private void Start()
        {
            closeButton.OnClickAsObservable().Take(1).Subscribe(_ =>
            {
                SoundManager.Instance.SaveVolumeBGM();
                SoundManager.Instance.SaveVolumeSE();
                VibrationManager.Instance.SaveEnable(vibrationalToggle.isOn);
                transform.parent.SetLocalScale(0);
                backgroundAnimator.SetTrigger(CloseTrigger);
                this.CallWaitForSeconds(.25f, () =>
                {
                    TransitionManager.Instance.UnloadScene(GameScenes.SettingsDialog);
                });
            }).AddTo(this);
            
            bgmVolumeSlider.value = SoundManager.Instance.GetVolume(SoundType.BGM);
            bgmVolumeSlider.OnValueChangedAsObservable().Subscribe(volume =>
            {
                var currentEnabled = SoundManager.Instance.IsEnabled(SoundType.BGM);
                var newEnabled = !Mathf.Approximately(volume, 0f);
                if (currentEnabled != newEnabled)
                {
                    SoundManager.Instance.EnabledSound(SoundType.BGM, newEnabled);
                    bgmSoundOnIcon.SetActive(newEnabled);
                }
                SoundManager.Instance.ChangeVolume(SoundType.BGM, volume);
            }).AddTo(this);
            
            seVolumeSlider.value = SoundManager.Instance.GetVolume(SoundType.SE);
            seVolumeSlider.OnValueChangedAsObservable().Subscribe(volume =>
            {
                var currentEnabled = SoundManager.Instance.IsEnabled(SoundType.SE);
                var newEnabled = !Mathf.Approximately(volume, 0f);
                if (currentEnabled != newEnabled)
                {
                    SoundManager.Instance.EnabledSound(SoundType.SE, newEnabled);
                    seSoundOnIcon.SetActive(newEnabled);
                }
                SoundManager.Instance.ChangeVolume(SoundType.SE, volume);
            }).AddTo(this);
            
            vibrationalToggle.OnValueChangedAsObservable().Subscribe(x =>
            {
                if (x)
                {
                    VibrationManager.Instance.PlayVibrationOnce();
                }
                vibeOnIcon.SetActive(x);
            }).AddTo(this);
            vibrationalToggle.isOn = VibrationManager.Instance.IsEnable;

            howToPlayButton.OnClickAsObservable().ThrottleFirst(TimeSpan.FromSeconds(.3f)).Subscribe(_ =>
            {
                TransitionManager.Instance.LoadSceneAdditive(GameScenes.HowToPlay);
            }).AddTo(this);
        }
    }
}