using XGame.Editor.Asset;
using System;
using System.IO;
using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Build.AssetBundles
{
    public class NormalBuildPipeline : IBuildPipeline
    {
        public bool BuildAssetBundles(UnityEditor.AssetBundleBuild[] builds)
        {
            var outputPath = PathDefine.AssetBundleOutputPath; // KFADF.FileUtil.WorkPath + "/AssetBundles";
            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }

            //LZ4压缩 | 报错中断
            //剔除文件头的Unity版本号信息 BuildAssetBundleOptions.AssetBundleStripUnityVersion
            var bundleOptions = BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.StrictMode;
            //            bool shouldCheckODR = EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS;
            //#if UNITY_TVOS
            //            shouldCheckODR |= EditorUserBuildSettings.activeBuildTarget == BuildTarget.tvOS;
            //#endif
            //            if (shouldCheckODR)
            //            {
            //#if ENABLE_IOS_ON_DEMAND_RESOURCES
            //                if (PlayerSettings.iOS.useOnDemandResources)
            //                    bundleOptions |= BuildAssetBundleOptions.UncompressedAssetBundle;
            //#endif
            //#if ENABLE_IOS_APP_SLICING
            //                bundleOptions |= BuildAssetBundleOptions.UncompressedAssetBundle;
            //#endif
            //            }
            return BuildAssetBundles(outputPath, builds, bundleOptions, EditorUserBuildSettings.activeBuildTarget);
        }

        public bool BuildAssetBundles(string outputPath, UnityEditor.AssetBundleBuild[] builds, BuildAssetBundleOptions assetBundleOptions, BuildTarget targetPlatform)
        {
            var manifest = BuildPipeline.BuildAssetBundles(outputPath, builds, assetBundleOptions, targetPlatform);
            if (manifest != null)
            {
                BuildLog.Log($"BuildAssetBundles success. Time: {DateTime.Now}");
                return true;
            }
            else
            {
                BuildLog.Error($"BuildAssetBundles failed.  Time: {DateTime.Now}");
            }
            return false;
        }
    }
}
