using System;
using UnityEngine;
using UnityEngine.UI;

namespace EscapeRoomEngine.Engine.Runtime.UI
{
    public class PersistedToggle : PersistedUI<bool>
    {
        [SerializeField] private Toggle toggle;

        protected override void Start()
        {
            base.Start();

            toggle.onValueChanged.AddListener(value =>
            {
                Value = value;
                PlayerPrefs.SetInt(key, Convert.ToInt32(Value));
                PlayerPrefs.Save();
            });
        }


        protected override bool RetrieveValue() => PlayerPrefs.GetInt(key, Convert.ToInt32(defaultValue)) != 0;
        
        protected override void DisplayValue()
        {
            toggle.isOn = Value;
        }
    }
}