using System;
using UnityEngine;
using UnityEngine.UI;
namespace Ashwild.Settings
{
///
/// A reusable toggle. Pure UI: it knows nothing about which bool setting it drives. The panel
/// calls once to register the action to run when the user flips it, then
/// reflects state through .
///
[DisallowMultipleComponent]
public class SettingToggleWidget : MonoBehaviour
{
#region Serialized Fields
[Header("References")]
[SerializeField] private Toggle toggle;
#endregion
#region State
private Action onChanged;
#endregion
#region Public API
///
/// Registers the action to run when the user flips the toggle. Call once.
///
public void Init(Action onChanged)
{
if (toggle == null)
{
Debug.LogError($"[SettingToggleWidget] '{name}' has no Toggle assigned.", this);
return;
}
this.onChanged = onChanged;
toggle.onValueChanged.RemoveListener(HandleToggleChanged);
toggle.onValueChanged.AddListener(HandleToggleChanged);
}
///
/// Displays the given state without invoking the apply action.
///
public void SetValue(bool value)
{
if (toggle != null) toggle.SetIsOnWithoutNotify(value);
}
#endregion
#region Event Handlers
///
/// Runs the apply action with the user-driven state.
///
private void HandleToggleChanged(bool value) => onChanged?.Invoke(value);
#endregion
}
}