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

namespace Thousandto.DIY
{
    public class ChangeUIFontEditor : EditorWindow
    {
        [MenuItem("Ares/ChangeSclectToNewFont")]
        static void ChangeToNewFont()
        {
            var selectObj = UnityEditor.Selection.gameObjects;
            if (selectObj == null || selectObj.Length <= 0)
                return;

            var baseFont = AssetDatabase.LoadAssetAtPath<UIFont>("Assets/GameAssets/Resources/GameUI/Base/Font/UIBaseFont.prefab");
            var newBaseFont = AssetDatabase.LoadAssetAtPath<UIFont>("Assets/GameAssets/Resources/GameUI/Base/Font/UINewMainFont.prefab");
            var newTitleFont = AssetDatabase.LoadAssetAtPath<UIFont>("Assets/GameAssets/Resources/GameUI/Base/Font/UINewTitleFont.prefab");
            if (baseFont == null || newBaseFont ==null || newTitleFont == null)
            {
                return;
            }
            for (int i = 0; i < selectObj.Length; ++i)
            {
                var go = selectObj[i];
                var labels = go.GetComponentsInChildren<UILabel>(true);
                if(labels != null && labels.Length > 0)
                {
                    for(int j = 0; j < labels.Length; ++j)
                    {
                        var label = labels[j];
                        if(label.bitmapFont == null || label.bitmapFont == baseFont)
                        {
                            label.bitmapFont = newBaseFont;
                        }
                        else if(label.bitmapFont != newBaseFont)
                        {
                            label.bitmapFont = newTitleFont;
                        }
                    }
                }

                var sprites = go.GetComponentsInChildren<UISprite>(true);
                if(sprites != null && sprites.Length > 0)
                {
                    for (int j = 0; j < sprites.Length; ++j)
                    {
                        var spr = sprites[j];
                        if(spr.atlas != null && spr.atlas.name.Contains("UIIconAtlas"))
                        {
                            var path = AssetDatabase.GetAssetPath(spr.atlas);
                            path = path.Replace("UIIconAtlas", "UINewIconAtlas");
                            var newatlas = AssetDatabase.LoadAssetAtPath<UIAtlas>(path);
                            if(newatlas != null)
                            {
                                spr.atlas = newatlas;
                            }
                        }
                    }
                }
            }
            UnityEngine.Debug.LogError("替换完成");
        }

        [MenuItem("Ares/ChangeUIFontEditor")]
        static void Open()
        {
            if (Application.isPlaying)
            {
                return;
            }

            var win = (ChangeUIFontEditor)EditorWindow.GetWindow(typeof(ChangeUIFontEditor));
            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 fightFont = AssetDatabase.LoadAssetAtPath<UIFont>("Assets/GameAssets/Resources/GameUI/Base/Font/UIFightFont.prefab");
                    var baseFont = AssetDatabase.LoadAssetAtPath<UIFont>("Assets/GameAssets/Resources/GameUI/Base/Font/UIBaseFont.prefab");
                    if (baseFont == null)
                    {
                        return;
                    }
                    for (int i = 0; i < files.Length; ++i)
                    {
                        var path = files[i].FullName.Replace('\\', '/');
                        path = path.Replace("D:/TianZhiJin/Client/Main/", "");
                        var go = AssetDatabase.LoadAssetAtPath<GameObject>(path); 
                        if (go != null)
                        {
                            var labels = go.GetComponentsInChildren<UILabel>(true);
                            for (int j = 0; j < labels.Length; ++j)
                            {
                                if (labels[j].bitmapFont == null)
                                {
                                    labels[j].bitmapFont = baseFont;
                                }
                            }
                            EditorUtility.SetDirty(go);
                        }
                        AssetDatabase.SaveAssets();
                        AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
                    }
                }
            }
        }
    }
}