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

namespace XGame.Editor.Build
{
    [BuildCommand((uint)BuildCommandPriority.BuildAssetBundle)]
    class CmdBuildAssetBundle : BaseBuildCommand, ICommandExecuterAsync
    {
        public void Execute()
        {
            //sbp推荐每次打包都是全量打包
            var abOutputPathPath = PathDefine.AssetBundleOutputPath;
            if (Directory.Exists(abOutputPathPath))
            {
                //直接删除文件夹。Directory.Exists在Windows会存在文件夹大小写的问题，如果该目录下刚好存在大小写不一致的同名文件夹，会导致后续文件判断出问题，保险起见，直接删了
                UnityEditor.FileUtil.DeleteFileOrDirectory(abOutputPathPath);
            }
            //if (isDistributedAB)
            //{
            //    UnityEditor.EditorApplication.LockReloadAssemblies();
            //    var mainNodeTask = new MainNodeTask(Context.config, OnMainNodeCompleted);
            //    mainNodeTask.Start();
            //    return;
            //}
            //资源打包成AB包
            if (XBuildPipeline.BuildAssetBundles())
            {
                this.Completed();
            }
            else
            {
                this.Completed(BuildErrorCode.BuildAssetBundleFailed);
            }
        }
        //private void OnMainNodeCompleted(bool isSuccess)
        //{
        //    UnityEditor.EditorApplication.UnlockReloadAssemblies();
        //    var code = BuildErrorCode.CmdCompleted;
        //    if (isSuccess)
        //    {
        //        //打包manifest
        //        if (!KBuildPipeline.BuildManifestBundle())
        //        {
        //            code = BuildErrorCode.BuildAssetBundleFailed;
        //            BuildLog.Error($"Build ManifestBundle failed.");
        //        }
        //    }
        //    else if (!KBuildPipeline.BuildAssetBundles())
        //    {
        //        code = BuildErrorCode.BuildAssetBundleFailed;
        //    }
        //    this.Completed(code);
        //}
    }
}
