using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class PanelSettings : BasePanel
{
    [SerializeField] CanvasGroup mCvsBg;
    [SerializeField] RectTransform mRctBg;

    [SerializeField] Button mBtnMask;
    [SerializeField] UIToggleSwap mTglBGM;
    [SerializeField] UIToggleSwap mTglSound;

    private void Awake()
    {
        UIUtils.BindBtn(mBtnMask, OnClickMask);
    }

    public override void OnOpen()
    {
        base.OnOpen();

        mTglBGM.OnSwitch += SwitchBGM;
        mTglSound.OnSwitch += SwitchSound;

        mTglBGM.IsOn = SettingData.Instance.IsMusicOn;
        mTglSound.IsOn = SettingData.Instance.IsSoundOn;

        mCvsBg.alpha = 0;
        mRctBg.anchoredPosition = new Vector2(0, -160);

        mCvsBg.DOFade(1, 0.3f);
        mRctBg.DOAnchorPosY(100, 0.3f);
    }

    public override void OnClose()
    {
        base.OnClose();

        AudioManager.Instance.PlaySound(AudioClipType.UIClose);

        mTglBGM.OnSwitch -= SwitchBGM;
        mTglSound.OnSwitch -= SwitchSound;
    }

    private void SwitchBGM(bool pIsOn)
    {
        SettingData.Instance.IsMusicOn = pIsOn;
        AudioManager.Instance.SetMusicOn(pIsOn);
    }

    private void SwitchSound(bool pIsOn)
    {
        SettingData.Instance.IsSoundOn = pIsOn;
        AudioManager.Instance.SetSoundOn(pIsOn);
    }

    private void OnClickMask()
    {
        mCvsBg.DOFade(0, 0.3f);
        mRctBg.DOAnchorPosY(-160, 0.3f).onComplete = Close;
    }
}