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

namespace Thousandto.DIY
{
    public class CheckUIResEditor : EditorWindow
    {
        [MenuItem("Ares/CheckUIResEditor")]
        static void Open()
        {
            if (Application.isPlaying)
            {
                return;
            }

            var win = (CheckUIResEditor)EditorWindow.GetWindow(typeof(CheckUIResEditor));
            win.Show();
        }

        private string _prefabPath = string.Empty;
        void OnGUI()
        {
            EditorGUILayout.BeginVertical();
            EditorGUILayout.LabelField("选择路径");
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("需要修改层的Prefab路径");
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("浏览", GUILayout.Width(50)))
            {
                _prefabPath = EditorUtility.OpenFolderPanel("Change Layer Prefab Path", _prefabPath, "");
            }
            if (string.IsNullOrEmpty(_prefabPath))
            {
                EditorGUILayout.LabelField("null...");
            }
            else
            {
                EditorGUILayout.LabelField(_prefabPath);
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();

            EditorGUILayout.BeginVertical();
            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();

            if (GUILayout.Button("检查", GUILayout.Width(50)))
            {
                if (string.IsNullOrEmpty(_prefabPath))
                    return;
                if (Directory.Exists(_prefabPath))
                {
                    DirectoryInfo direction = new DirectoryInfo(_prefabPath);
                    FileInfo[] files = direction.GetFiles("*.prefab", SearchOption.AllDirectories);

                    var text = new StringBuilder(1024 * 1024);
                    var text2 = new StringBuilder(1024 * 1024);
                    for (int i = 0; i < files.Length; ++i)
                    {
                        var path = files[i].FullName.Replace('\\', '/');
                        var starIndex = path.IndexOf("/Assets/", 0);
                        path = path.Remove(0, starIndex + 1);
                        var go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                        if (go != null)
                        {
                            text2.Length = 0;
                            var labels = go.GetComponentsInChildren<UILabel>(true);
                            for (int j = 0; j < labels.Length; ++j)
                            {
                                if (labels[j].bitmapFont != null && !labels[j].bitmapFont.name.Contains("UINew"))
                                {
                                    text2.AppendFormat(",{0}", labels[j].name);
                                }
                            }

                            var sprites = go.GetComponentsInChildren<UISprite>(true);
                            for (int j = 0; j < sprites.Length; ++j)
                            {
                                if (sprites[j].atlas != null && !sprites[j].atlas.name.Contains("UINew"))
                                {
                                    text2.AppendFormat(",{0}", sprites[j].name);
                                }
                            }

                            if(text2.Length > 0)
                            {
                                text.AppendFormat("\n{0}", go.name);
                                text.Append(text2);
                            }
                        }
                    }
                    var savePath = EditorUtility.OpenFilePanel("选择保存的文件", "", "*.txt");
                    if(!string.IsNullOrEmpty(savePath))
                    {
                        File.WriteAllText(savePath, text.ToString());
                    }
                }
            }
        }
    }
}