using System.Collections.Generic;
using System.IO;
using MotionFramework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;

namespace DefaultNamespace
{
    /// <summary>
    /// 数据管理
    /// </summary>
    public class DataConfigManager : ModuleSingleton<DataConfigManager>, IModule
    {
        private string ToolsPackDataPath = Application.streamingAssetsPath + "/DataConfig/ToolsPackData.json"; //工具包
        private string TeachingDataPath = Application.streamingAssetsPath + "/DataConfig/TeachingData.json"; //教学工具包设备高亮数据

        private Dictionary<string, List<string>> toolsPackDict;
        private List<string> teachingData;


        [SerializeField] private ProcessMode.ProcessMode _processMode;


        public void OnCreate(object createParam)
        {
            LoadConfig();

            // LoadTeachingData();
        }

        public void OnUpdate()
        {
        }

        public void OnDestroy()
        {
        }

        public void OnGUI()
        {
        }

        public void SetProcessMode(int index)
        {
            if (index == 0)// 练习模式 
            {
                _processMode = DefaultNamespace.ProcessMode.ProcessMode.Practice;
            }
            else if (index == 1)// 考核模式
            {
                _processMode = DefaultNamespace.ProcessMode.ProcessMode.Assessment;
            }
            else if (index == 2)// 教学模式
            {
                _processMode = DefaultNamespace.ProcessMode.ProcessMode.Teaching;
            }
            else if (index == 3)// 培训模式
            {
                _processMode = DefaultNamespace.ProcessMode.ProcessMode.Training;
            }
            
            
            
            Debug.Log("当前流程模式---->"+_processMode);
        }

        public ProcessMode.ProcessMode GetProcessMode()
        {
            return _processMode;
        }

        /// <summary>
        /// 获取教学工具包设备高亮数据
        /// </summary>
        /// <returns></returns>
        public List<string> GetTeachingData()
        {
            return teachingData;
        }

        private void LoadTeachingData()
        {
            string jsonContent = File.ReadAllText(ToolsPackDataPath);
            teachingData = JsonConvert.DeserializeObject<List<string>>(jsonContent);
            foreach (var tool in teachingData)
            {
                Debug.Log("Tool model: " + tool);
            }
        }

        private void LoadConfig()
        {
            if (File.Exists(ToolsPackDataPath))
            {
                string jsonContent = File.ReadAllText(ToolsPackDataPath);
                JObject jsonObj = JObject.Parse(jsonContent);

                toolsPackDict = new Dictionary<string, List<string>>();

                var tools = jsonObj["tools"] as JObject;
                if (tools != null)
                {
                    ParseTools(tools["models"] as JArray);
                }

                // Debug.Log("工具和子项:");
                // foreach (var item in toolsDict)
                // {
                //     Debug.Log($"{item.Key}:");
                //     foreach (var subItem in item.Value)
                //     {
                //         Debug.Log($"  - {subItem}");
                //     }
                // }
            }
            else
            {
            }
        }


        public List<string> GetToolsPackData(string toolsName)
        {
            foreach (var v in toolsPackDict)
            {
                if (v.Key == toolsName)
                {
                    if (v.Value.Count > 0)
                    {
                        return v.Value;
                    }
                    else
                    {
                        return null;
                    }
                }
            }

            return null;
        }

        void ParseTools(JArray models)
        {
            foreach (var model in models)
            {
                if (model is JValue)
                {
                    string modelName = model.ToString();
                    if (!toolsPackDict.ContainsKey(modelName))
                    {
                        toolsPackDict[modelName] = new List<string>();
                    }
                }
                else if (model is JObject subTool)
                {
                    string subToolName = subTool["name"].ToString();
                    var subItems = subTool["models"] ?? subTool["items"];
                    List<string> subItemList = new List<string>();
                    if (subItems is JArray subArray)
                    {
                        foreach (var subItem in subArray)
                        {
                            subItemList.Add(subItem.ToString());
                        }
                    }

                    toolsPackDict[subToolName] = subItemList;
                    if (subItems is JArray)
                    {
                        continue;
                    }

                    ParseTools(subItems as JArray); // 递归解析子对象
                }
            }
        }
    }
}