// 本脚本只在不存在时会生成一次。组件逻辑写在当前脚本内。已存在不会再次生成覆盖 

using FairyGUI;
using NBC;
using NBF.Setting;
using UnityEngine.InputSystem;

namespace NBF
{
    public partial class SettingInputItem : GButton
    {
        public InputOption Option;
        private InputActionRebindingExtensions.RebindingOperation rebindingOperation;


        private void OnInited()
        {
            BtnKeyboard.onClick.Set(OnClickRebind);
            onChanged.Add(SelfChanged);
        }

        public void SetData(InputOption option)
        {
            Option = option;
            TextName.SetLanguage(Option.Name);
            UpdateValueText();
        }

        public void OnClickRebind()
        {
            // InputWaitingPanel.Show(true);
            // UI.Inst.OpenUI<InputWaitingPanel>();
            SettingWaitInputPanel.Show();
            // // 取消当前绑定
            // var settingPanel = UI.Inst.GetUI<SettingPanel>();
            // settingPanel.InputWait.visible = true;
            // Option.InputAction.Disable();
            //
            // // 开始重绑定操作
            // rebindingOperation = Option.InputAction.PerformInteractiveRebinding(Option.BindingIndex)
            //     .OnMatchWaitForAnother(0.1f)
            //     .OnComplete(operation => RebindComplete())
            //     .OnCancel(operation => RebindCanceled())
            //     .Start();
        }

        private void RebindComplete()
        {
            rebindingOperation.Dispose();
            UpdateValueText();
            Option.InputAction.Enable();
        }

        private void RebindCanceled()
        {
            rebindingOperation.Dispose();
            Option.InputAction.Enable();
            // UI.Inst.HideUI<InputWaitingPanel>();
        }


        private void SelfChanged(EventContext context)
        {
            if (!selected)
            {
                // RebindOver();
            }
        }

        private void UpdateValueText()
        {
            BtnKeyboard.SetData(Option);
        }
    }
}