using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using WS;

///<summary>自动对于UI路径</summary>
public class UIPathEditor : EditorWindow
{
    [MenuItem("Tools/Framework/自动对应UI路径")]
    public static void ShowVoice()
    {
        UIPathEditor window = EditorWindow.GetWindow<UIPathEditor>("对应UI路径");
        window.Show();
    }

    ///<summary>加载路径</summary>
    private string loadpath = "Assets/Resources/UI/";
    ///<summary>配置文件</summary>
    private List<ResourcePath> Config = new List<ResourcePath>();
    ///<summary>滑杆</summary>
    private Vector2 pos;
    ///<summary>路径</summary>
    private string path;

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

    private void OnGUI()
    {
        EditorGUILayout.BeginHorizontal();
        GUILayout.Label("");
        if (GUILayout.Button("自动对应", GUILayout.Width(100)))
        {
            Config.Clear();
            List<string> allfilepath = new List<string>();
            string[] temp = Directory.GetFiles(loadpath, "*.prefab", SearchOption.AllDirectories);
            allfilepath.AddRange(temp);
            ToSetString(allfilepath);
            for (int i = 0; i < allfilepath.Count; i++)
            {
                FileInfo fi = new FileInfo(allfilepath[i]);
                string filename = fi.Name.Replace(fi.Extension, "");
                if (!Config.Exists(v => v.Name == filename))
                {
                    ResourcePath pathdata = new ResourcePath();
                    pathdata.Name = filename;
                    pathdata.Path = allfilepath[i].Replace("Assets/Resources/", "").Replace(".prefab", "");
                    Config.Add(pathdata);
                }
                else
                {
                    ShowNotification(new GUIContent($"{filename} 已存在"), 2);
                }
            }
            ConfigHelper.SaveConfig(Config, path);
            EditorHelper.TypeToScript<View>("Assets/Scripts/Framework/MVC/View", typeof(ViewManager));
            AssetDatabase.Refresh();
        }
        GUILayout.Label("");
        EditorGUILayout.EndHorizontal();

        GUILayout.Label("");

        pos = EditorGUILayout.BeginScrollView(pos);
        for (int i = 0; i < Config.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("View 名称", GUILayout.Width(70));
            GUILayout.Label(Config[i].Name, GUILayout.Width(200));
            GUILayout.Label("View 路径", GUILayout.Width(70));
            GUILayout.Label(Config[i].Path);
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.EndScrollView();
    }

    ///<summary>转意符</summary>
    private void ToSetString(List<string> list)
    {
        for (int i = 0; i < list.Count; i++)
        {
            list[i] = list[i].Replace(@"\", "/");
        }
    }
}