using System.IO;
using UnityEditor;
using UnityEngine;

namespace NBF
{
    /// <summary>
    /// 打包ab
    /// </summary>
    [BuildTaskInfo(BuildTaskId.AB, "打包AB")]
    public class BuildABTask : BaseBuildTask
    {
        public override void Run(BuildContext context)
        {
            if (context.Ver != PlayerSettings.bundleVersion)
            {
                PlayerSettings.bundleVersion = context.Ver;
            }

            DeleteStreamingAssetsBundleCache();
            NBC.Asset.Editor.Builder.Build();
           // Builder.Build();
        }

        void DeleteStreamingAssetsBundleCache()
        {
            var mainFolder = Path.Combine(Application.streamingAssetsPath, "main").Replace("\\", "/");
            if (Directory.Exists(mainFolder))
                Directory.Delete(mainFolder, true);
            string[] files = Directory.GetFiles(Application.streamingAssetsPath);
            foreach (string file in files)
            {
                var filePath = file.Replace("\\", "/");
                string fileName = Path.GetFileName(filePath);
                if (fileName.Contains("packages_") || fileName.Contains("version"))
                    File.Delete(filePath);
            }

            AssetDatabase.Refresh();
        }
    }
}