using System;
using UnityEngine.UI;
namespace UnityEngine.Rendering.UI
{
    /// 
    /// DebugUIHandler for indirect toggle widget.
    /// 
    public class DebugUIHandlerIndirectToggle : DebugUIHandlerWidget
    {
        /// 
        /// Label of the widget.
        /// 
        public Text nameLabel;
        /// Toggle of the toggle field.
        public Toggle valueToggle;
        /// Checkmark image.
        public Image checkmarkImage;
        /// 
        /// Getter function for this indirect widget.
        /// 
        public Func getter;
        /// 
        /// Setter function for this indirect widget.
        /// 
        public Action setter;
        // Should not be here, this is a byproduct of the Bitfield UI Handler implementation.
        internal int index;
        /// 
        /// Initialize the indirect widget.
        /// 
        public void Init()
        {
            UpdateValueLabel();
            valueToggle.onValueChanged.AddListener(OnToggleValueChanged);
        }
        void OnToggleValueChanged(bool value)
        {
            setter(index, value);
        }
        /// 
        /// OnSelection implementation.
        /// 
        /// True if the selection wrapped around.
        /// Previous widget.
        /// True if the selection is allowed.
        public override bool OnSelection(bool fromNext, DebugUIHandlerWidget previous)
        {
            nameLabel.color = colorSelected;
            checkmarkImage.color = colorSelected;
            return true;
        }
        /// 
        /// OnDeselection implementation.
        /// 
        public override void OnDeselection()
        {
            nameLabel.color = colorDefault;
            checkmarkImage.color = colorDefault;
        }
        /// 
        /// OnAction implementation.
        /// 
        public override void OnAction()
        {
            bool value = !getter(index);
            setter(index, value);
            UpdateValueLabel();
        }
        internal void UpdateValueLabel()
        {
            if (valueToggle != null)
                valueToggle.isOn = getter(index);
        }
    }
}