using XGame.Framework.Json;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;

namespace XGame.Editor.Asset.Tools
{
    public class PrefabDependenciesTask
    {
        private class PrefabInfo
        {
            public string assetPath;
            public string[] dependencies;
        }
        private HashSet<string> _prefabPaths;
        private List<string> _tempDependencies;
        private List<PrefabInfo> _prefabInfos;
        public void Run()
        {
            Init();
            FindDependencies();
            Export();
        }
        private void Init()
        {
            var assetRoots = FileUtil.GetProductAssetsRoots();
            var prefabGUIDs = AssetDatabase.FindAssets("t:Prefab", assetRoots);
            _prefabPaths = new HashSet<string>();
            foreach (var guid in prefabGUIDs)
            {
                var assetPath = AssetDatabase.GUIDToAssetPath(guid);
                _prefabPaths.Add(assetPath);
            }
            _tempDependencies = new List<string>();
            _prefabInfos = new List<PrefabInfo>();
        }
        private void FindDependencies()
        {
            var index = 0;
            var count = _prefabPaths.Count;
            foreach (var assetPath in _prefabPaths)
            {
                EditorUtility.DisplayProgressBar("检测可加载资源的预制依赖...", $"Path:{assetPath}", (++index) / (float)count);
                FindDependencies(assetPath);
            }
            EditorUtility.ClearProgressBar();
        }
        private void FindDependencies(string assetPath)
        {
            // 只检测直接依赖
            var deps = AssetDatabase.GetDependencies(assetPath, false);
            foreach (var dep in deps)
            {
                if (dep == assetPath)
                {
                    continue;
                }
                if (_prefabPaths.Contains(dep))
                {
                    _tempDependencies.Add(dep);
                }
            }
            if (_tempDependencies.Count > 0)
            {
                _prefabInfos.Add(new PrefabInfo()
                {
                    assetPath = assetPath,
                    dependencies = _tempDependencies.ToArray()
                });
                _tempDependencies.Clear();
            }
        }
        private void Export()
        {
            try
            {
                if (_prefabInfos.Count == 0)
                {
                    UnityEngine.Debug.Log("PrefabDependencies Completed.");
                    return;
                }
                var str = XJson.ToJson(_prefabInfos);
                var path = Path.Combine(Path.GetTempPath(), "PrefabDependencies.json");
                File.WriteAllText(path, str);
                UnityEngine.Debug.Log($"PrefabDependencies 输出路径:{path}");
#if UNITY_EDITOR_WIN
                var proc = System.Diagnostics.Process.Start("notepad.exe", path);
#endif
            }
            catch (Exception e)
            {
                UnityEngine.Debug.LogException(e);
            }
        }
    }
}
