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

namespace Stary.Evo.Editor
{
    [Serializable]
    public class SimulateBuildPipelineViewer:AbstractBuildPipelineViewer
    {
        public SimulateBuildPipelineViewer(BuildAssetDataSetting packageName) : base(packageName)
        {
            isSimulate = 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);
            
            EditorSimulateBuildParameters buildParameters = new EditorSimulateBuildParameters();
            buildParameters.BuildOutputRoot = AssetBundleBuilderHelper.GetDefaultBuildOutputRoot();
            buildParameters.BuildinFileRoot = AssetBundleBuilderHelper.GetStreamingAssetsRoot();
            buildParameters.BuildPipeline = dataSetting.selectedBuildPipelines.ToString();
            buildParameters.BuildBundleType = (int)EBuildBundleType.VirtualBundle;
            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.EncryptionServices = CreateEncryptionInstance();
            
            EditorSimulateBuildPipeline pipeline = new EditorSimulateBuildPipeline();
            var buildResult = pipeline.Run(buildParameters, true);
            if (buildResult.Success)
                EditorUtility.RevealInFinder(buildResult.OutputPackageDirectory);
        }
    }
}