using System;
using UnityEditor;
using YooAsset;
using YooAsset.Editor;

namespace Stary.Evo.Editor
{
    [Serializable]
    public class BuiltinBuildPipelineViewer :AbstractBuildPipelineViewer
    {
        public BuiltinBuildPipelineViewer(BuildAssetDataSetting packageName) : base(packageName)
        {
        }

        public override void ExecuteBuild()
        {
            var fileNameStyle = AssetBundleBuilderSetting.GetPackageFileNameStyle(dataSetting.packageName, dataSetting.selectedBuildPipelines);
            var buildinFileCopyOption = AssetBundleBuilderSetting.GetPackageBuildinFileCopyOption(dataSetting.packageName, dataSetting.selectedBuildPipelines);
            var buildinFileCopyParams = AssetBundleBuilderSetting.GetPackageBuildinFileCopyParams(dataSetting.packageName, dataSetting.selectedBuildPipelines);
            var compressOption = AssetBundleBuilderSetting.GetPackageCompressOption(dataSetting.packageName, dataSetting.selectedBuildPipelines);
            var clearBuildCache = AssetBundleBuilderSetting.GetPackageClearBuildCache(dataSetting.packageName, dataSetting.selectedBuildPipelines);
            var useAssetDependencyDB = AssetBundleBuilderSetting.GetPackageUseAssetDependencyDB(dataSetting.packageName, dataSetting.selectedBuildPipelines);

            BuiltinBuildParameters buildParameters = new BuiltinBuildParameters();
            buildParameters.BuildOutputRoot = AssetBundleBuilderHelper.GetDefaultBuildOutputRoot();
            buildParameters.BuildinFileRoot = AssetBundleBuilderHelper.GetStreamingAssetsRoot();
            buildParameters.BuildPipeline = dataSetting.selectedBuildPipelines.ToString();
            buildParameters.BuildBundleType = (int)EBuildBundleType.AssetBundle;
            buildParameters.BuildTarget = EditorUserBuildSettings.activeBuildTarget;
            buildParameters.PackageName = dataSetting.packageName;
            buildParameters.PackageVersion =dataSetting.GetBuildPackageVersion();
            buildParameters.EnableSharePackRule = true;
            buildParameters.VerifyBuildingResult = true;
            buildParameters.FileNameStyle = fileNameStyle;
            buildParameters.BuildinFileCopyOption = buildinFileCopyOption;
            buildParameters.BuildinFileCopyParams = buildinFileCopyParams;
            buildParameters.CompressOption = compressOption;
            buildParameters.ClearBuildCacheFiles = clearBuildCache;
            buildParameters.UseAssetDependencyDB = useAssetDependencyDB;
            buildParameters.EncryptionServices = CreateEncryptionInstance();

            BuiltinBuildPipeline pipeline = new BuiltinBuildPipeline();
            var buildResult = pipeline.Run(buildParameters, true);
            if (buildResult.Success)
                EditorUtility.RevealInFinder(buildResult.OutputPackageDirectory);
            base.ExecuteBuild();
        }
    }
}