using UnityEngine;
using UnityEngine.UI;

namespace EscapeRoomEngine.Engine.Runtime.UI
{
    public class PersistedSlider : PersistedUI<int>
    {
        [SerializeField] private Slider slider;
        [SerializeField] private Text label;

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

            slider.wholeNumbers = true;

            slider.onValueChanged.AddListener(value =>
            {
                Value = (int) value;
                DisplayValue();
                PlayerPrefs.SetInt(key, Value);
                PlayerPrefs.Save();
            });
        }


        protected override int RetrieveValue() => PlayerPrefs.GetInt(key, defaultValue);
        
        protected override void DisplayValue()
        {
            slider.value = Value;
            label.text = Value.ToString();
        }
    }
}