using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using WS;
public class CourseEditor : EditorWindow
{
    [MenuItem("Tools/课程编辑器")]
    public static void ShowWindow()
    {
        CourseEditor window = EditorWindow.GetWindow<CourseEditor>();
        window.Show();
    }

    ///<summary>课程</summary>
    private List<CourseData> Config;
    ///<summary>文件路径</summary>
    private string path;
    ///<summary>课程名称引用</summary>
    private string CourseName;


    private List<string> textAll = new List<string>() { };

    private void OnEnable()
    {
        path = $"{Application.dataPath}/Resources/Config/Framework/Course.txt";
        if (ConfigHelper.ConfigExists(path))
        {
            Config = ConfigHelper.FileLoadConfig<List<CourseData>>(path);
        }
    }

    private void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        CourseName = EditorGUILayout.TextField(CourseName);
        if (GUILayout.Button("添加", GUILayout.Width(100)))
        {
            if (string.IsNullOrEmpty(CourseName))
            {
                Debug.LogError("名称不能为空!");
            }
            else
            {
                CourseData data = Config.Find(c => c.Name == CourseName);
                if (data != null)
                {
                    Debug.LogError($"课程 {CourseName} 已存在!");
                }
                else
                {
                    Config.Add(new CourseData { Name = CourseName });
                    CourseName = null;
                }
            }
        }
        EditorGUILayout.EndHorizontal();
        for (int i = 0; i < Config.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            Config[i].Name = EditorGUILayout.TextField(Config[i].Name);
            if (GUILayout.Button("删除", GUILayout.Width(100)))
            {
                Config.RemoveAt(i);
            }
            if (GUILayout.Button("编辑", GUILayout.Width(100)))
            {
                ProcessEditor window = EditorWindow.GetWindow<ProcessEditor>();
                window.Init(Config[i].Name);
                window.Show();
                //Close();
            }
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("");
        if (GUILayout.Button("保存", GUILayout.Width(100)))
        {
            ConfigHelper.SaveConfig(Config, path);
        }
        EditorGUILayout.EndHorizontal();



        EditorGUILayout.BeginHorizontal();
        if (GUILayout.Button("合并", GUILayout.Width(100)))
        {
            string text = null;
            List<Module> modules = new List<Module>();
            modules.Add(new Module());
            modules[0].Id = 1;
            modules[0].Name = "AllProcess";
            modules[0].NoOrder = false;
            modules[0].NoOrder = false;
            for (int i = 0; i < 6; i++)
            {
                text = Resources.Load<TextAsset>("Config/Process/Process" + (i + 1)).text;
                var m = LitJson.JsonMapper.ToObject<List<Module>>(text);
                modules[0].WSTasks.AddRange(m[0].WSTasks);
            }
            for (int i = 0; i < modules[0].WSTasks.Count; i++)
            {
                modules[0].WSTasks[i].Id = i + 1;
            }
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(LitJson.JsonMapper.ToJson(modules)); ;
            FileStream fs = new FileStream(Application.dataPath + "/Resources/Config/Process/AllProcess.txt", FileMode.Create);
            fs.Write(bytes, 0, bytes.Length);
            fs.Close();
            Debug.Log("合并成功");
        }
        EditorGUILayout.EndHorizontal();
    }
}