using System;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using Stary.Evo.Editor;
using UnityEditor;
using UnityEngine;
using YooAsset;
using YooAsset.Editor;

namespace Stary.Evo.Editor
{
    [Serializable]
    public class AbstractBuildPipelineViewer
    {
        protected BuildAssetDataSetting dataSetting;

        protected bool isSimulate;
        protected bool isRaw;
        
        [Title("清理构建缓存", titleAlignment: TitleAlignments.Centered)]
        [HorizontalGroup("BuildCache")]
        [InfoBox("当不勾选此项的时候，引擎会开启增量打包模式，会极大提高构建速度！")]
        [OnValueChanged("SetBuildCacheToggle")]
        [HideIf("isSimulate", true)]
        public bool clearBuildCacheToggle;

        [Title("依赖数据库", titleAlignment: TitleAlignments.Centered)]
        [HorizontalGroup("BuildCache")]
        [InfoBox("当开启此项的时候，会极大提高构建速度！")]
        [OnValueChanged("SetUseAssetDependencyDB")]
        [HideIf("isSimulate", true)]
        public bool useAssetDependencyDBToggle;

        [Title("资源包的压缩方式", titleAlignment: TitleAlignments.Centered)]
        [HorizontalGroup("FileStyle"), HideLabel]
        [OnValueChanged("SetCompression")]
        [HideIf("@isSimulate==true || isRaw==true")]
        public ECompressOption compression;

        [Title("资源包文件名称样式", titleAlignment: TitleAlignments.Centered)]
        [HorizontalGroup("FileStyle"), HideLabel]
        [OnValueChanged("SetFileNameStyle")]
        [HideIf("isSimulate", true)]
        public EFileNameStyle fileNameStyle;

        [Title("首包资源文件的拷贝方式", titleAlignment: TitleAlignments.Centered)]
        [HorizontalGroup("FileStyle"), HideLabel]
        [OnValueChanged("SetCopyBuildinFileOption")]
        [HideIf("isSimulate", true)]
        public EBuildinFileCopyOption copyBuildinFileOption;

        [Title("首包资源文件的拷贝参数", titleAlignment: TitleAlignments.Centered)]
        [HideLabel]
        [OnValueChanged("SetCopyBuildinFileParams")]
        [ShowIf(
            "@copyBuildinFileOption==EBuildinFileCopyOption.ClearAndCopyByTags||copyBuildinFileOption==EBuildinFileCopyOption.OnlyCopyByTags ")]
        public string copyBuildinFileParams;

        public AbstractBuildPipelineViewer(BuildAssetDataSetting dataSetting)
        {
            this.dataSetting = dataSetting;
            useAssetDependencyDBToggle = true;
            clearBuildCacheToggle = false;
            compression = ECompressOption.LZ4;
            fileNameStyle = EFileNameStyle.BundleName;
            copyBuildinFileOption = EBuildinFileCopyOption.ClearAndCopyAll;
        }

        #region AssetBuild

        private void SetCompression()
        {
            AssetBundleBuilderSetting.SetPackageCompressOption(dataSetting.packageName, dataSetting.selectedBuildPipelines,
                compression);
        }

        private void SetFileNameStyle()
        {
     
            AssetBundleBuilderSetting.SetPackageFileNameStyle(dataSetting.packageName, dataSetting.selectedBuildPipelines,
                fileNameStyle);
        }

        private void SetCopyBuildinFileOption()
        {

            AssetBundleBuilderSetting.SetPackageBuildinFileCopyOption(dataSetting.packageName,
                dataSetting.selectedBuildPipelines, copyBuildinFileOption);

            //PlayerPrefs.SetInt("CopyBuildinFileOption", (int)copyBuildinFileOption);
        }

        private void SetCopyBuildinFileParams()
        {

            AssetBundleBuilderSetting.SetPackageBuildinFileCopyParams(dataSetting.packageName,
                dataSetting.selectedBuildPipelines, copyBuildinFileParams);
            //PlayerPrefs.SetInt("CopyBuildinFileOption", (int)copyBuildinFileOption);
        }

        public void SetBuildCacheToggle()
        {
            AssetBundleBuilderSetting.SetPackageClearBuildCache(dataSetting.packageName, dataSetting.selectedBuildPipelines,
                clearBuildCacheToggle);
        }

        private void SetUseAssetDependencyDB()
        {

            AssetBundleBuilderSetting.SetPackageUseAssetDependencyDB(dataSetting.packageName, dataSetting.selectedBuildPipelines,
                useAssetDependencyDBToggle);
        }

        #endregion



        public virtual void ExecuteBuild()
        {
            //  GetPackageVersion();
        }


        /// <summary>
        /// 创建加密类实例
        /// </summary>
        protected IEncryptionServices CreateEncryptionInstance()
        {
            var encyptionClassName =
                AssetBundleBuilderSetting.GetPackageEncyptionClassName(dataSetting.packageName,
                    dataSetting.selectedBuildPipelines);
            var encryptionClassTypes = EditorTools.GetAssignableTypes(typeof(IEncryptionServices));
            var classType = encryptionClassTypes.Find(x => x.FullName.Equals(encyptionClassName));
            if (classType != null)
                return (IEncryptionServices)Activator.CreateInstance(classType);
            else
                return null;
        }


    }
}