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

namespace XGame.Editor.Asset.Tools
{
    public class I18nAssetsReferencesTask
    {
        private string[] _i18nAssetRoots;
        private HashSet<string> _i18nAssetPaths;
        private HashSet<string> _preAssetPaths;
        public void Run()
        {
            CollectI18nAssets();
            CollectAddressableAssets();
            var errorMsgs = VerifyAssetReferences();
            if (errorMsgs.Count == 0)
            {
                UnityEngine.Debug.Log($"I18nAssetsReferences 查找结束.");
                return;
            }
            Export(errorMsgs);
        }

        private void CollectI18nAssets()
        {
            _i18nAssetRoots = FileUtil.GetI18nAssetsRoots();
            _i18nAssetPaths = new HashSet<string>();

            var assetGuids = AssetDatabase.FindAssets("", _i18nAssetRoots);
            foreach (var assetGuid in assetGuids)
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
                if (Directory.Exists(assetPath) || FileUtil.IsFileIgnore(assetPath))
                {
                    continue;
                }
                _i18nAssetPaths.Add(assetPath);
            }
        }
        private void CollectAddressableAssets()
        {
            _preAssetPaths = new HashSet<string>();
            var assetGuids = AssetDatabase.FindAssets("", new string[] { PathDefine.ResAddressableRelative });
            foreach (var assetGuid in assetGuids)
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
                if (Directory.Exists(assetPath) || FileUtil.IsFileIgnore(assetPath) || FileUtil.IsTextureFile(assetPath))
                {
                    continue;
                }
                _preAssetPaths.Add(assetPath);
            }
        }
        private List<string> VerifyAssetReferences()
        {
            var errorMsgs = new List<string>();
            foreach (var assetPath in _preAssetPaths)
            {
                var deps = AssetDatabase.GetDependencies(assetPath, true);
                foreach (var dep in deps)
                {
                    if (_i18nAssetPaths.Contains(dep))
                    {
                        errorMsgs.Add($"资源:{assetPath} 错误依赖:{dep}");
                    }
                }
            }
            return errorMsgs;
        }
        /// <summary>
        /// 把日志内容显示在记事本中
        /// </summary>
        public void Export(List<string> errorMsgs)
        {
            try
            {
                var str = string.Join("\n", errorMsgs);
                var path = Path.Combine(Path.GetTempPath(), "I18nAssetsReferencesError.txt");
                File.WriteAllText(path, str);
                UnityEngine.Debug.Log($"I18nAssetsReferences 输出路径:{path}");
#if UNITY_EDITOR_WIN
                var proc = System.Diagnostics.Process.Start("notepad.exe", path);
#endif
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogException(e);
            }
        }

    }
}
