#if UNITY_EDITOR
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine.U2D;

namespace UnityEngine.UI.PackgTool
{
    public static class UGUICacheInfo
    {
        public static List<SpriteAtlas> atlasList
        {
            get
            {
                int cunt = GetAtlasFileCount();
                if (_atlasList == null || cunt != atlasCount)
                {
                    atlasCount = cunt;
                    SetAtlasList();
                }


                return _atlasList;
            }
        }

        public static UGUIPackDB uguiPackDB
        {
            get
            {
                if (_uguiPackDB == null)
                {
                    SetUGUIPackDB();
                }

                return _uguiPackDB;
            }
        }

        private static int atlasCount;

        private static List<SpriteAtlas> _atlasList;
        private static UGUIPackDB _uguiPackDB;

        private static Dictionary<string, List<SpriteAtlas>> imagePacks = new Dictionary<string, List<SpriteAtlas>>();
        public static List<TextrueJson> AllTextrueJsons
        {
            get
            {
                if (_allTextrueJsons == null)
                {
                    _allTextrueJsons = new List<TextrueJson>();
                    
                }

                return _allTextrueJsons;
            }
        }

        public static List<TextrueJson> _allTextrueJsons;
        public static List<SpriteAtlas> GetAllSpriteAtlas(string name)
        {
            if (imagePacks.ContainsKey(name))
            {
                return imagePacks[name];
            }

            List<SpriteAtlas> spriteAtlasList = new List<SpriteAtlas>();
            for (int i = 0; i < atlasList.Count; i++)
            {
                Sprite atlasSprite = atlasList[i].GetSprite(name);
                if (atlasSprite != null)
                {
                    spriteAtlasList.Add(atlasList[i]);
                }
            }
            imagePacks.Add(name,spriteAtlasList);
            return spriteAtlasList;
        }
        public static void Clare()
        {
            _uguiPackDB = null;
            _atlasList = null;
          
        }

        public static TextrueJson GetTongYong(string textrueName)
        {
            for (int i = 0; i < AllTextrueJsons.Count; i++)
            {
                TextrueJson textrueJson=  AllTextrueJsons[i];
                if (textrueJson.isTyPack)
                {
                    for (int j = 0; j < textrueJson.newTextureJson.Count; j++)
                    {
                        if (textrueJson.newTextureJson[j].textrueName.Equals(textrueName))
                        {
                            return textrueJson;
                        }
                    }
                }
            }

            return null;
        }

        private static int GetAtlasFileCount()
        {
            string[] allPath = System.IO.Directory.GetFiles(Application.dataPath + "/Res/UIAtlas/", "*.spriteatlasv2");
            return allPath.Length;
        }

        private static void SetAtlasList()
        {
            _atlasList = new List<SpriteAtlas>();
            string[] allPath = System.IO.Directory.GetFiles(Application.dataPath + "/Res/UIAtlas/", "*.spriteatlasv2");
            for (int i = 0; i < allPath.Length; i++)
            {
                if (allPath[i].Contains(".meta"))
                {
                    continue;
                }

                string p = allPath[i].Replace(Application.dataPath, "Assets");
                SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(p);
                if (spriteAtlas != null)
                {
                    _atlasList.Add(spriteAtlas);
                }
            }
        }

        public static PackInfo GetPackDB(string packName)
        {
            for (int i = 0; i < uguiPackDB.allPackgInfos.Count; i++)
            {
                if (uguiPackDB.allPackgInfos[i].packName.Equals(packName))
                {
                    return uguiPackDB.allPackgInfos[i];
                }
            }

            return null;
        }

        public static PackInfo AddNewPackInfo(string jsonPath, TextrueJson textrueJson)
        {
            PackInfo packInfo = new PackInfo();
            packInfo.packgJsonPath = jsonPath;
            packInfo.textrueJson = textrueJson;
            packInfo.packName = textrueJson.packName;
            packInfo.spriteAtlas = GetSpriteAtlas( textrueJson.packName);
            uguiPackDB.allPackgInfos.Add(packInfo);
            string json = JsonUtility.ToJson(uguiPackDB);
            string packgPath = Application.dataPath.Replace("Assets", "UGUIPack/uguiconfig.db");
            File.WriteAllText(packgPath, json);
            return packInfo;
        }

        public static SpriteAtlas GetSpriteAtlas(string packName)
        {
            List<SpriteAtlas> spriteAtlasList=  atlasList;
            for (int i = 0; i < spriteAtlasList.Count; i++)
            {
                if (spriteAtlasList[i].name.Equals(packName))
                {
                    return spriteAtlasList[i];
                }
            }

            return null;
        }

        public static void SaveDB()
        {
            string packgPath = Application.dataPath.Replace("Assets", "UGUIPack/uguiconfig.db");
            string json = JsonUtility.ToJson(UGUICacheInfo.uguiPackDB);
            File.WriteAllText(packgPath, json);
        }

        private static void SetUGUIPackDB()
        {
            string packgPath = Application.dataPath.Replace("Assets", "UGUIPack/");
            if (!System.IO.Directory.Exists(packgPath))
            {
                Directory.CreateDirectory(packgPath);
            }

            string dbPath = packgPath + "uguiconfig.db";

            if (!File.Exists(dbPath))
            {
                _uguiPackDB = new UGUIPackDB();
            }
            else
            {
                string json = File.ReadAllText(dbPath);
                _uguiPackDB = JsonUtility.FromJson<UGUIPackDB>(json);
            }
        }
    }
}
#endif