using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
using UnityEngine.UI.PackgTool;

namespace UnityEditor.UI
{
    public class UGUIGameObjectTool
    {
        private static List<SpriteAtlas> allSpriteAtlasList;

        public static void ReviewUI(List<SpriteAtlas> allSpriteAtlasList, SpriteAtlas spriteAtlas, PackInfo packInfo)
        {
            UGUIGameObjectTool.allSpriteAtlasList = allSpriteAtlasList;
            string[] allFinle =
                System.IO.Directory.GetFiles(Application.dataPath + "/Res/UI/", "*", SearchOption.AllDirectories);
            // string[] uiallObj = AssetDatabase.FindAssets("", new string[] {"Assets/Res/UI"});
            for (int i = 0; i < allFinle.Length; i++)
            {
                string path = allFinle[i].Replace(Application.dataPath, "Assets");
                if (path.Contains(".mate"))
                {
                    continue;
                }

                GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                if (go != null)
                {
                    bool isAlter = ReviewGameObject(go, spriteAtlas, packInfo);
                    if (isAlter)
                    {
                        PrefabUtility.SavePrefabAsset(go);
                    }
                }
            }

            AssetDatabase.SaveAssets();
        }


        private static SpriteAtlas GetSpriteNamePack(string spriteName)
        {
            if (allSpriteAtlasList == null || allSpriteAtlasList.Count <= 0)
            {
                return null;
            }

            for (int i = 0; i < allSpriteAtlasList.Count; i++)
            {
                if (allSpriteAtlasList[i].GetSprite(spriteName) != null)
                {
                    return allSpriteAtlasList[i];
                }
            }

            return null;
        }

        private static bool ReviewGameObject(GameObject gameObject, SpriteAtlas spriteAtlas, PackInfo packInfo)
        {
            bool isAlter = false;
            MyImage[] allImage = gameObject.transform.GetComponentsInChildren<MyImage>(true);
            if (allImage == null)
            {
                return false;
            }

            for (int i = 0; i < allImage.Length; i++)
            {
                MyImage image = allImage[i];
                bool isoK = false;
                if (image.packInfo != null && image.CurrSpriteAtlas != null)
                {
                    isoK = image.CurrSpriteAtlas.GetSprite(image.icon_name) != null;
                    if (!isoK) //没在在图集里面找到图片了，需要去其他地方查找(删除图集)
                    {
                        SpriteAtlas newAtlas = GetSpriteNamePack(image.icon_name);
                        if (newAtlas != null)
                        {
                            image.packInfo = UGUICacheInfo.uguiPackDB.GetPackgInfo(newAtlas.name);
                            image.CurrSpriteAtlas = newAtlas;
                            image.icon_name = image.icon_name;
                            image.ReashUI();
                            isAlter = true;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }

                if (image.CurrSpriteAtlas == null &&
                    image.sprite != null|| !string.IsNullOrEmpty( image.icon_name)) //没有图集 但是由功能管理器添加的UI （添加图集）
                {
                    string name = image.sprite!=null? image.sprite!.name: image.icon_name;
                    SpriteAtlas newAtlas = GetSpriteNamePack(name);
                    // Sprite sprite = spriteAtlas.GetSprite(name);
                    if (newAtlas != null)
                    {
                        image.packInfo = UGUICacheInfo.uguiPackDB.GetPackgInfo(newAtlas.name);
                        image.CurrSpriteAtlas = newAtlas;
                        image.icon_name = image.icon_name;
                        image.ReashUI();
                        isAlter = true;
                    }
                }
            }

            return isAlter;
        }
    }
}