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

namespace Stary.Evo.Editor
{
    [CreateAssetMenu(fileName = "BuildAssetDataSetting", menuName = "Evo/BuildAssetDataSetting")]
    public class BuildAssetDataSetting : SerializedScriptableObject
    {
        public void OnEnable()
        {
            buildTarget = EditorUserBuildSettings.activeBuildTarget.ToString();
            GetPackageVersion();
            oldVersionX = packageVersionX;
            oldVersionY = packageVersionY;
            oldVersionZ = packageVersionZ;
        }

        [HideInInspector] public string packageName;
        [HideInInspector]public int packageVersionX = 1;
        [HideInInspector]public int packageVersionY;
        [HideInInspector]public int packageVersionZ;
        private int oldVersionX = 1;
        private int oldVersionY;
        private int oldVersionZ;

        [Title("目标环境", titleAlignment: TitleAlignments.Centered)]
        [HorizontalGroup("BuildSetting"), HideLabel]
        [OnValueChanged("SetEnvironmentType")]
        public EnvironmentType environmentType;

        [Title("当前打包平台", titleAlignment: TitleAlignments.Centered)]
        [ReadOnly]
        [HorizontalGroup("BuildSetting"), HideLabel]
        public string buildTarget;

        private const string VerSersionMessage = "(小版本由打包叠加，中版本、大版本更新会重置小版本，如误操作，点击重置按钮即可)";

        [TitleGroup("当前打包版本号", alignment: TitleAlignments.Centered)]
        [ReadOnly]
        [HorizontalGroup("当前打包版本号/PackageVersion"), HideLabel]
        public string packageVersion;

        [HorizontalGroup("当前打包版本号/PackageVersion"), HideLabel, EnumToggleButtons]
        [HideIf("environmentType", EnvironmentType.Debug)]
        public VersionPosType VersionType;


        [HorizontalGroup("当前打包版本号/PackageVersion"), HideLabel, Button(Icon = SdfIconType.Plus)]
        [HideIf("environmentType", EnvironmentType.Debug)]
        public void AddBigVersion()
        {
            if (VersionType == VersionPosType.X)
            {
                packageVersionX++;
            }
            else if (VersionType == VersionPosType.Y)
            {
                packageVersionY++;
            }

            if (packageVersionZ > 0)
                packageVersionZ = 0;
            GetPackageVersion();
        }

        [HorizontalGroup("当前打包版本号/PackageVersion"), HideLabel, Button(Icon = SdfIconType.Dash)]
        [HideIf("environmentType", EnvironmentType.Debug)]
        public void SubBigVersion()
        {
            if (VersionType == VersionPosType.X)
            {
                packageVersionX--;
            }
            else if (VersionType == VersionPosType.Y)
            {
                packageVersionY--;
            }

            if (packageVersionZ > 0)
                packageVersionZ = 0;
            GetPackageVersion();
        }

        [HorizontalGroup("当前打包版本号/PackageVersion"), HideLabel, Button(Icon = SdfIconType.ArrowRepeat)]
        [HideIf("environmentType", EnvironmentType.Debug)]
        public void RepeatVersion()
        {
            packageVersionX = oldVersionX;
            packageVersionY = oldVersionY;
            packageVersionZ = oldVersionZ;
            GetPackageVersion();
        }

        [Title("构建列表", titleAlignment: TitleAlignments.Centered)]
        [HorizontalGroup("BuildPipeline"), HideLabel]
        [OnValueChanged("SetBuildPipelines")]
        public EBuildPipeline selectedBuildPipelines;

        [Title("加密列表", titleAlignment: TitleAlignments.Centered)]
        [HorizontalGroup("BuildPipeline"), HideLabel]
        [ValueDropdown("GetEncryptionServices")]
        [OnValueChanged("SetEncryptionServices")]
        public Type encryption;

        [HideLabel]  public AbstractBuildPipelineViewer viewer;

        [HideLabel]  private Dictionary<EBuildPipeline, AbstractBuildPipelineViewer> _viewers;

        public void SetEnvironmentType()
        {
            GetPackageVersion();
        }

        public string GetBuildPackageVersion(bool isUpdate = false)
        {
            if (environmentType == EnvironmentType.Debug)
            {
                return "Simulate";
            }

            if (environmentType == EnvironmentType.Release)
            {
                string version =
                    $"{packageVersionX}.{packageVersionY}.{packageVersionZ}";

                if (isUpdate)
                    packageVersionZ++;

                packageVersion = $"{packageVersionX}.{packageVersionY}.{packageVersionZ}";
                return version;
            }

            return null;
        }

        public void GetPackageVersion()
        {
            SetBuildPipelines();
            if (environmentType == EnvironmentType.Debug)
            {
                viewer.clearBuildCacheToggle = true;
                selectedBuildPipelines = EBuildPipeline.EditorSimulateBuildPipeline;
                packageVersion = "Simulate";
                
            }
            else if (environmentType == EnvironmentType.Release)
            {
                viewer.clearBuildCacheToggle = false;
                selectedBuildPipelines = EBuildPipeline.BuiltinBuildPipeline;

                packageVersionX = packageVersionX > 1 ? packageVersionX : 1;
                packageVersionY =
                    packageVersionY > 0 ? packageVersionY : 0;

                packageVersion =
                    $"{packageVersionX}.{packageVersionY}.{packageVersionZ}";
            }

            viewer.SetBuildCacheToggle();

            AssetDatabase.SaveAssets();
            //BuildServerPath();
        }

        /// <summary>
        /// 设置当前构建管线
        /// </summary>
        /// <returns></returns>
        private void SetBuildPipelines()
        {
            BuildPipelineViewer();
        }

        /// <summary>
        /// 生成打包实例
        /// </summary>
        public void BuildPipelineViewer()
        {
            if (_viewers == null)
                _viewers = new Dictionary<EBuildPipeline, AbstractBuildPipelineViewer>();
            if (_viewers.ContainsKey(selectedBuildPipelines))
            {
                viewer = _viewers[selectedBuildPipelines];
            }
            else
            {
                switch (selectedBuildPipelines)
                {
                    case EBuildPipeline.EditorSimulateBuildPipeline:
                        viewer = new SimulateBuildPipelineViewer(this);
                        break;
                    case EBuildPipeline.BuiltinBuildPipeline:
                        viewer = new BuiltinBuildPipelineViewer(this);
                        break;
                    case EBuildPipeline.ScriptableBuildPipeline:
                        viewer = new ScriptableBuildPipelineViewer(this);
                        break;
                    case EBuildPipeline.RawFileBuildPipeline:
                        viewer = new RawfileBuildpipelineViewer(this);
                        break;
                }

                _viewers.Add(selectedBuildPipelines, viewer);
            }
        }

        private List<Type> GetEncryptionServices()
        {
            var encryptionClassTypes = EditorTools.GetAssignableTypes(typeof(IEncryptionServices));
            if (encryption.IsNull())
            {
                encryption = encryptionClassTypes[0];
            }

            return encryptionClassTypes;
        }

        private void SetEncryptionServices()
        {
            AssetBundleBuilderSetting.SetPackageEncyptionClassName(packageName, selectedBuildPipelines,
                encryption.FullName);
        }
    }

    public enum VersionPosType
    {
        X,
        Y,
    }

    public enum EnvironmentType
    {
        Debug,
        Release,
    }
}