using XGame.Editor.Build.AssetBundles;
using XGame.Editor.Asset;
using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Build
{
    public static class XBuildPipeline
    {
        [MenuItem("Build/一键打包", priority = 1)]
        private static void BuildApp()
        {
            ////收集资源信息
            XBuildPipeline.GenerateAddressable();
            ////创建图集配置
            //CreateAtlas();
            ////资源打包成AB包
            //BuildBundles();
            ////拷贝资源
            //CopyStreamAssets();
            //打包App
            BuildAppWindow.CreateWindow();
        }
        public static void GenerateAddressable()
        {
            var buildTask = new AddressableBuildTask();
            buildTask.Run();
            //CollectCustomAddressableNames();
            BuildLog.Log($"GenerateAddressable finish. Time: {System.DateTime.Now}");

        }
        [MenuItem("Build/CreateAtlas", priority = 21)]
        public static void GenerateAtlas()
        {
            var task = new AtlasBuildTask();
            task.Run();
        }

        [MenuItem("Build/BuildAssetBundles", priority = 22)]
        public static bool BuildAssetBundles()
        {
            var builder = new BundleContextBuilder();
            if (!builder.Build())
            {
                return false;
            }
            var context = builder.Product;
            var controller = BundleManifestsController.Creat(context);
            var pipeline = new SbpBuildPipeline();
            var buildTask = new AssetBundleBuildTask(context, controller, pipeline);
            return buildTask.Run();
        }
        //[MenuItem("Build/SerializeConfigs", priority = 23)]
        private static void SerializeConfigs()
        {
            SerializableUtils.SerializeAllConfigs();
        }
        [MenuItem("Build/ExportAssets", priority = 24)]
        private static void ExportAssets()
        {
            var context = new BuildContext()
            {
                config = new BuildConfig()
                {
                    editor = new BuildConfig.EditorSettings()
                    {
                        isNative = true
                    },
                    project = new BuildConfig.ProjectSettings()
                    {
                        outputPath = PathDefine.DefaultOutputPath
                    }
                }
            };
            IBuildCommand cmd = new CmdExportAssets();
            if (cmd is IBuildContextAdapter adapter)
            {
                adapter.Context = context;
            }
            cmd.Start();
        }
        [MenuItem("Build/BuildPlayer", priority = 25)]
        private static void BuildPlayer()
        {
            LocalizedBuild.BuildPlayer();
        }
        [MenuItem("Build/CreatePackerManifest", priority = 46)]
        private static void CreatePackerManifest()
        {
            ABPackerInfoManifest.LoadOrCreate();
        }

        [MenuItem("Build/GenAssetbundleManifests", priority = 47)]
        public static bool GenerateBundleManifests()
        {
            var builder = new BundleContextBuilder();
            if (!builder.Build())
            {
                return false;
            }
            var controller = BundleManifestsController.Creat(builder.Product);
            return controller.GenerateManifests();
        }
        [MenuItem("Build/BuildManifestBundle", priority = 48)]
        public static bool BuildManifestBundle()
        {
            // sbp打包速度很快，一般不用单独打manifest的包
            var pipeline = new NormalBuildPipeline();
            var task = new ManifestBundleBuildTask(pipeline);
            return task.Run();
        }
        [MenuItem("Build/反序列化Manifest", priority = 101)]
        private static void DeserializeABManifest()
        {
            DeserializeABManifestWindow.CreateWindow();
        }
        [MenuItem("Build/重新编译代码", priority = 102)]
        private static void RequestScriptCompilation()
        {
            UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation();
        }

        [MenuItem("Build/Assetbundle分组/常规")]
        public static bool GenAssetbundleGroupsNormal()
        {
            var outputPath = $"{PathDefine.ExtAssetsPath}/AbGroupsNormal.json";
            var task = new AssetBundleGroupsTask(outputPath, AssetBundleGroupsTask.AbGroupMode.Normal);
            return task.Excute();
        }
        [MenuItem("Build/Assetbundle分组/依赖顺序")]
        public static bool GenAssetbundlesByDependencies()
        {
            var outputPath = $"{PathDefine.ExtAssetsPath}/AbGroupsByDependencies.json";
            var task = new AssetBundleGroupsTask(outputPath, AssetBundleGroupsTask.AbGroupMode.Dependencies);
            return task.Excute();
        }
    }
}

