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

namespace Stary.Evo.Editor
{
    [Serializable]
    public class RawfileBuildpipelineViewer:AbstractBuildPipelineViewer
    {
        public RawfileBuildpipelineViewer(BuildAssetDataSetting packageName) : base(packageName)
        {
            isRaw = true;
        }
        

        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 clearBuildCache = AssetBundleBuilderSetting.GetPackageClearBuildCache(dataSetting.packageName, dataSetting.selectedBuildPipelines);
            var useAssetDependencyDB = AssetBundleBuilderSetting.GetPackageUseAssetDependencyDB(dataSetting.packageName, dataSetting.selectedBuildPipelines);

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

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