using System;
using System.Collections;
using System.Collections.Generic;
using DefaultNamespace;
using MotionFramework;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;


namespace ToolsPack
{
    public enum ModelTypeEnum
    {
        工具,
        设备,
        材料
    }

    /// <summary>
    /// 工具窗口管理器
    /// </summary>
    public class ToolsPackWindowManager : MonoBehaviour
    {
        private Dictionary<string, List<GameObject>> _toolsNames;
        private Dictionary<string, List<ToolsPackString>> _toolsNamesString;
        private Dictionary<string, ToolsPackWindowItemBtComponent> _toolsNameTemp;

        [SerializeField] private Transform content;
        [SerializeField] private Toggle tog;

        public static bool isOpenBag = false;

        public void OnEnable()
        {
            // ChangePage(ModelTypeEnum.工具);

            StartCoroutine(SetToggleOnNextFrame(tog));
            isOpenBag = true;
        }

        private IEnumerator SetToggleOnNextFrame(Toggle toggle)
        {
            // 等待一帧，确保所有初始化完成
            yield return null;

            // 设置 Toggle 的选中状态
            // 设置 isOn 属性为 true
            toggle.isOn = true;
            toggle.onValueChanged.Invoke(true);
        }

        private void OnDisable()
        {
            isOpenBag = false;

        }


        /// <summary>
        /// Toggle切换
        /// </summary>
        public void ChangePage(ModelTypeEnum windw)
        {
            int count = content.childCount;
            for (int i = 0; i < count; i++)
            {
                Destroy(content.GetChild(i).gameObject);
            }

            MotionEngine.GetModule<ToolsPackManager>().ClearToolsPackWindowItemBts();
            _toolsNameTemp = new Dictionary<string, ToolsPackWindowItemBtComponent>();

            if (MotionEngine.GetModule<ToolsPackManager>().GetToolsPackScene() == ToolsPackScene.工具间)
            {
                //初始化工具集
                _toolsNames = MotionEngine.GetModule<ToolsPackManager>().GetToolsPack();

                GameObject bt = MotionEngine.GetModule<ToolsPackManager>().GetToolsPackWindowBt();
                foreach (var v in _toolsNames)
                {
                    List<string> li = MotionEngine.GetModule<DataConfigManager>().GetToolsPackData(v.Key);

                    if (li == null)
                    {
                        foreach (var b in v.Value)
                        {
                            ToolsPackGameObjectComponent tp = b.GetComponent<ToolsPackGameObjectComponent>();
                            if (_toolsNameTemp.ContainsKey(v.Key))
                            {
                                ToolsPackWindowItemBtComponent btComponent;
                                if (_toolsNameTemp.TryGetValue(v.Key, out btComponent))
                                {
                                    btComponent.AddGameNumber(v.Key, tp.GetIndex());
                                }
                            }
                            else
                            {
                                if (tp.GetModelTypeEnum() == windw)
                                {
                                    ToolsPackWindowItemBtComponent btComponent = Instantiate(bt, content, false).GetComponent<ToolsPackWindowItemBtComponent>();
                                    btComponent.name = v.Key;
                                    btComponent.Init(v.Key, v.Key, tp.GetIndex(), tp.GetModelTypeEnum());
                                    MotionEngine.GetModule<ToolsPackManager>().AddToolsPackWindowItemBts(btComponent.gameObject);
                                    _toolsNameTemp.Add(v.Key, btComponent);

                                    List<string> wearToolsTemp = MotionEngine.GetModule<ToolsPackManager>().wearTools;
                                    if (wearToolsTemp.Contains(v.Key))
                                    {
                                        btComponent.gameObject.SetActive(false);
                                    }
                                }
                            }


                        }
                    }
                    else
                    {
                        foreach (var lis in v.Value)
                        {
                            if (lis.GetComponent<ToolsPackGameObjectComponent>().GetModelTypeEnum() == windw)
                            {
                                foreach (var to in li)
                                {
                                    ToolsPackGameObjectComponent tp = lis.GetComponent<ToolsPackGameObjectComponent>();
                                    if (_toolsNameTemp.ContainsKey(to))
                                    {
                                        ToolsPackWindowItemBtComponent btComponent1;
                                        if (_toolsNameTemp.TryGetValue(to, out btComponent1))
                                        {
                                            btComponent1.AddGameNumber(v.Key, tp.GetIndex());
                                        }
                                    }
                                    else
                                    {
                                        ToolsPackWindowItemBtComponent btComponent = Instantiate(bt, content, false).GetComponent<ToolsPackWindowItemBtComponent>();
                                        btComponent.name = to;
                                        btComponent.Init(to, v.Key, tp.GetIndex(), tp.GetModelTypeEnum());
                                        MotionEngine.GetModule<ToolsPackManager>().AddToolsPackWindowItemBts(btComponent.gameObject);
                                        _toolsNameTemp.Add(to, btComponent);
                                        List<string> wearToolsTemp = MotionEngine.GetModule<ToolsPackManager>().wearTools;
                                        if (wearToolsTemp.Contains(to))
                                        {
                                            btComponent.gameObject.SetActive(false);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                _toolsNamesString = MotionEngine.GetModule<ToolsPackManager>().GetToolsPackString();
                GameObject bt = MotionEngine.GetModule<ToolsPackManager>().GetToolsPackWindowBt();
                foreach (var v in _toolsNamesString)
                {
                    List<string> li = MotionEngine.GetModule<DataConfigManager>().GetToolsPackData(v.Key);

                    if (li == null)
                    {
                        foreach (var b in v.Value)
                        {
                            if (_toolsNameTemp.ContainsKey(v.Key))
                            {
                                ToolsPackWindowItemBtComponent btComponent;
                                if (_toolsNameTemp.TryGetValue(v.Key, out btComponent))
                                {
                                    btComponent.AddGameNumber(v.Key, b.Index);
                                }
                            }
                            else
                            {
                                if (b.ModelTypeEnum == windw)
                                {
                                    ToolsPackWindowItemBtComponent btComponent = Instantiate(bt, content, false).GetComponent<ToolsPackWindowItemBtComponent>();
                                    btComponent.name = v.Key;
                                    btComponent.Init(v.Key, v.Key, b.Index, b.ModelTypeEnum);
                                    MotionEngine.GetModule<ToolsPackManager>().AddToolsPackWindowItemBts(btComponent.gameObject);
                                    _toolsNameTemp.Add(v.Key, btComponent);
                                    List<string> wearToolsTemp = MotionEngine.GetModule<ToolsPackManager>().wearTools;
                                    if (wearToolsTemp.Contains(v.Key))
                                    {
                                        btComponent.gameObject.SetActive(false);
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (var lis in v.Value)
                        {
                            if (lis.ModelTypeEnum == windw)
                            {
                                foreach (var to in li)
                                {
                                    if (_toolsNameTemp.ContainsKey(to))
                                    {
                                        ToolsPackWindowItemBtComponent btComponent1;
                                        if (_toolsNameTemp.TryGetValue(to, out btComponent1))
                                        {
                                            btComponent1.AddGameNumber(v.Key, lis.Index);
                                        }
                                    }
                                    else
                                    {
                                        ToolsPackWindowItemBtComponent btComponent = Instantiate(bt, content, false).GetComponent<ToolsPackWindowItemBtComponent>();
                                        btComponent.name = to;
                                        btComponent.Init(to, v.Key, lis.Index, lis.ModelTypeEnum);
                                        List<string> wearToolsTemp = MotionEngine.GetModule<ToolsPackManager>().wearTools;
                                        if (wearToolsTemp.Contains(v.Key))
                                        {
                                            btComponent.gameObject.SetActive(false);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}