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

[RequireComponent(typeof(Toggle))]
public class PlayerPrefToggle : MonoBehaviour
{
    private Toggle _toggle;

    [SerializeField]
    private PlayerPrefValue _playerPref;
    
    [SerializeField]
    private bool _defaultValue;
    
    private void Awake()
    {
        _toggle = GetComponent<Toggle>();
        _toggle.onValueChanged.AddListener( ( ticked ) => _playerPref.SetInt( ticked ? 1 : 0 ) );
    }

    private void OnEnable()
    {
        bool value = _playerPref.HasValue ? _playerPref.GetInt() > 0 : _defaultValue;
        _toggle.isOn = value;
    }
}
