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

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

namespace NBF
{
    public partial class SettingItem : GButton
    {
        public OptionBase Option;

        private void OnInited()
        {
            BtnPrev.onClick.Set(OnPrev);
            BtnNext.onClick.Set(OnNext);
            BtnControl1.onClick.Set(OnControl);
            Slider.onChanged.Set(OnSliderChanged);
            ComboBox.onChanged.Set(OnComboBoxChanged);
        }

        public void SetData(OptionBase option)
        {
            Option = option;
            if (option is IMultiOption multiOption)
            {
                if (multiOption.IsDropdown)
                {
                    style.selectedIndex = 2;
                    if (multiOption.IsDropdown)
                    {
                        ComboBox.items = multiOption.GetOptionNames().ToArray();
                        ComboBox.selectedIndex = multiOption.GetValue();
                    }
                }
                else
                {
                    style.selectedIndex = 1;
                }
            }
            else if (option is RangeOption range)
            {
                style.selectedIndex = 3; //进度类型
                Slider.min = range.MinValue;
                Slider.max = range.MaxValue;
                Slider.wholeNumbers = true;
            }
            else if (option is KeyBoardOption keyBoardOption)
            {
                style.selectedIndex = 4; //按键类型
            }
            else
            {
                style.selectedIndex = 0;
            }

            TextName.SetLanguage(Option.Name);
            SetShow();
        }

        private void SetShow()
        {
            UpdateValueText();
        }

        public void OnPrev()
        {
            if (Option is IMultiOption multiOption)
            {
                var index = multiOption.GetValue();
                if (index > 0)
                {
                    index--;
                }
                else if (index == 0)
                {
                    var names = multiOption.GetOptionNames();
                    index = names.Count - 1;
                }

                if (Option is ResolutionSetting resolutionSetting)
                {
                    var res = Screen.resolutions;
                    Debug.LogError(
                        $"next index={index} count = {multiOption.GetOptionNames().Count} res={res.Length}");
                }

                multiOption.SetValue(index);
            }
            else if (Option is RangeOption range)
            {
                var value = range.GetValue();
                value--;
                if (value < range.MinValue)
                {
                    value = range.MinValue;
                }

                range.SetValue(value);
                Slider.value = value;
                UpdateValueText();
            }

            SetShow();
        }

        public void OnNext()
        {
            if (Option is IMultiOption multiOption)
            {
                var names = multiOption.GetOptionNames();
                var index = multiOption.GetValue();
                if (index < names.Count - 1)
                {
                    index++;
                }
                else if (index == names.Count - 1)
                {
                    index = 0;
                }

                if (Option is ResolutionSetting resolutionSetting)
                {
                    var res = Screen.resolutions;
                    Debug.LogError($"next index={index} count = {names.Count} res={res.Length}");
                }

                multiOption.SetValue(index);
            }
            else if (Option is RangeOption range)
            {
                var value = range.GetValue();
                value++;
                if (value > range.MaxValue)
                {
                    value = range.MaxValue;
                }

                range.SetValue(value);
                Slider.value = value;
                UpdateValueText();
            }


            SetShow();
        }

        public void OnSliderChanged()
        {
            if (Option is RangeOption range)
            {
                range.SetValue((int)Slider.value);
                UpdateValueText();
            }
        }

        private void OnComboBoxChanged()
        {
            Debug.Log(ComboBox.selectedIndex);
            if (Option is IMultiOption multiOption)
            {
                multiOption.SetValue(ComboBox.selectedIndex);
            }
        }

        public void OnControl()
        {
            SettingWaitInputPanel.Show(this);
        }

        public void UpdateValueText()
        {
            Pages.visible = false;
            if (Option is RangeOption range)
            {
                Slider.value = range.GetValue();
                TextSliderValue.text = range.GetDisplayString();
            }
            else if (Option is IMultiOption multiOption)
            {
                TextInfo.text = multiOption.GetDisplayString();
                Pages.visible = true;
                var count = multiOption.GetOptionNames().Count;
                if (Pages.TotalPages != count)
                {
                    Pages.SetTotal(count);
                }

                Pages.SetCurrent(multiOption.GetValue());
            }
            else if (Option is KeyBoardOption keyBoardOption)
            {
                UpdateControlInfo();
            }
        }

        private void UpdateControlInfo()
        {
            // BtnControl
            if (Option is KeyBoardOption keyBoardOption)
            {
                if (keyBoardOption.InputAction.name.Contains("Bob"))
                {
                    int i = 0;
                }
                BtnControl1.SetData(0, keyBoardOption);
            }
            
        }
    }
}