using XGame.Editor.Asset;
using System;
using System.Collections.Generic;
using System.IO;
using XGame.Framework.Json;
namespace XGame.Editor.Build.AssetBundles
{
    internal static class EChartsExporter
    {
        public struct NodeCategory
        {
            public string name;
            public string keyword;
        }
        public struct AbNode
        {
            public string name;
            public int value;
            public int category;
        }
        public struct NodeLink
        {
            public int source;
            public int target;
        }
        public class AbNodes
        {
            public string type;
            public NodeCategory[] categories;
            public AbNode[] nodes;
            public NodeLink[] links;
        }

        [UnityEditor.MenuItem("Build/Assetbundle分组/导出配置(ECharts)")]
        public static void GenAbNodes()
        {
            var outputPath = $"{PathDefine.ExtAssetsPath}/AssetbundleNodes.json";
            var manifest = AssetBundleUtils.LoadManifest();
            if (manifest == null || manifest.bundleInfos == null)
                return;
            var bundleInfos = manifest.bundleInfos;
            var task = new AssetBundleGroupsTask(string.Empty, AssetBundleGroupsTask.AbGroupMode.Dependencies);
            if (!task.BundleGroup(bundleInfos, out var bundleGroupMap))
            {
                return;
            }
            var nodes = new List<AbNode>();
            var links = new List<NodeLink>();
            var bundleIndexMap = new Dictionary<uint, int>();
            var groupMax = 0;
            for (var i = 0; i < bundleInfos.Length; i++)
            {
                var bundleInfo = bundleInfos[i];
                var groupId = bundleGroupMap[bundleInfo.bundleId];
                if (groupId > groupMax)
                    groupMax = groupId;
                var node = new AbNode()
                {
                    name = bundleInfo.bundleId.ToString(),
                    value = groupId,
                    category = groupId,
                };
                nodes.Add(node);
                bundleIndexMap.Add(bundleInfo.bundleId, i);
            }
            var dependencies = new List<uint>();
            foreach (var bundleInfo in bundleInfos)
            {
                if (bundleInfo.DependenciesCount > 0)
                {
                    var sourceIdx = bundleIndexMap[bundleInfo.bundleId];
                    dependencies.AddRange(bundleInfo.dependencies);
                    foreach (var depId in bundleInfo.dependencies)
                    {
                        var targetIdx = bundleIndexMap[depId];
                        var tarBundle = bundleInfos[targetIdx];
                        for (int i = dependencies.Count - 1; i >= 0; i--)
                        {
                            var compareId = dependencies[i];
                            if (compareId == depId)
                                continue;
                            if (tarBundle.DependenciesCount > 0 && Array.IndexOf(tarBundle.dependencies, compareId) >= 0)
                            {
                                dependencies.RemoveAt(i);
                            }
                        }
                    }
                    foreach (var dep in dependencies)
                    {
                        var link = new NodeLink()
                        {
                            source = sourceIdx,
                            target = bundleIndexMap[dep]
                        };
                        links.Add(link);
                    }
                    dependencies.Clear();
                }
            }
            //导出
            var categories = new NodeCategory[groupMax + 1];
            for (var i = 0; i < categories.Length; i++)
            {
                categories[i] = new NodeCategory()
                {
                    name = i.ToString()
                };
            }
            var abNodes = new AbNodes()
            {
                type = "force",
                categories = categories,
                nodes = nodes.ToArray(),
                links = links.ToArray()
            };
            var text = XJson.ToJson(abNodes);
            File.WriteAllText(outputPath, text, System.Text.Encoding.UTF8);
            BuildLog.Log($"GenAssetbundleNodes Path:{outputPath}");
        }

    }

}
