using UnityEditor;
using UnityEngine;

namespace XGame.Editor.Build
{
    [BuildCommand((uint)BuildCommandPriority.SetPlayerSettings)]
    internal class CmdSetPlayerSettings : BaseBuildCommand, ICommandExecuterAsync
    {
        public void Execute()
        {
            BuildLog.Log("CmdSetPlayerSettings Execute.");
            var config = Context.config;
            PlayerSettings.productName = config.project.productName;
            PlayerSettings.applicationIdentifier = config.project.applicationIdentifier;

            PlayerSettings.bundleVersion = Context.version.ToString();
            SetVersionCode(config.target, config.project.versionCode);
            EditorUserBuildSettings.exportAsGoogleAndroidProject = config.android.exportAsGoogleAndroidProject;
            EditorUserBuildSettings.androidCreateSymbols = (AndroidCreateSymbols)config.androidCreateSymbols;
            //release
            var isDebug = config.publishType == PublishType.Debug;
            EditorUserBuildSettings.development = config.publishType != PublishType.Release;
            EditorUserBuildSettings.connectProfiler = config.publishType == PublishType.Profiler;
            EditorUserBuildSettings.allowDebugging = config.publishType == PublishType.Profiler;
            //StackTrace
            //None: 不向日志输出堆栈跟踪。
            //ScriptOnly: 仅输出托管堆栈跟踪。
            //Full: 将记录原生和托管堆栈跟踪。
            PlayerSettings.SetStackTraceLogType(LogType.Log, isDebug ? StackTraceLogType.ScriptOnly : StackTraceLogType.None);
            PlayerSettings.SetStackTraceLogType(LogType.Assert, isDebug ? StackTraceLogType.ScriptOnly : StackTraceLogType.None);
            PlayerSettings.SetStackTraceLogType(LogType.Warning, isDebug ? StackTraceLogType.ScriptOnly : StackTraceLogType.None);
            PlayerSettings.SetStackTraceLogType(LogType.Error, StackTraceLogType.ScriptOnly);
            PlayerSettings.SetStackTraceLogType(LogType.Exception, StackTraceLogType.Full);

            var group = Context.config.target.ToTargetGroup();

            var scriptImpl = Context.config.scriptImpl;
            PlayerSettings.SetIl2CppCompilerConfiguration(group, isDebug ? Il2CppCompilerConfiguration.Debug : Il2CppCompilerConfiguration.Release);
            if (group != BuildTargetGroup.iOS)
            { //iOS固定为IL2CPP，并且不用设置
                PlayerSettings.SetScriptingBackend(group, scriptImpl.ToUnityImpl());
            }
            if (group == BuildTargetGroup.Android)
            {
                var architecture = Context.config.android.architecture;
                if (architecture == AndroidArchitecture.None) architecture = AndroidArchitecture.ARMv7;
                PlayerSettings.Android.targetArchitectures = architecture.ToUnityImpl();
            }
            else if (group == BuildTargetGroup.iOS)
            {
                PlayerSettings.SetArchitecture(group, (int)AppleMobileArchitecture.ARM64);
            }

            var targetLevel = UnityEditor.ApiCompatibilityLevel.NET_4_6;
            if (targetLevel == PlayerSettings.GetApiCompatibilityLevel(group))
            {
                //AssetDatabase.SaveAssets();
                //AssetDatabase.Refresh();
                //打包机没启动编辑器界面，不会主动保存配置，强制重新编译脚本以保存配置
                //UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation();
                this.Completed();
            }
            else
            {
                BuildCompileEvent.RegistEvent(Context);
                BuildLog.Log($"PlayerSettings SetApiCompatibilityLevel. Level:{targetLevel}");
                PlayerSettings.SetApiCompatibilityLevel(group, targetLevel);
            }
        }
        private void SetVersionCode(BuildTarget target, string versionCode)
        {
            switch (target)
            {
                case BuildTarget.Android:
                    int.TryParse(versionCode, out var code);
                    PlayerSettings.Android.bundleVersionCode = code;
                    break;
                case BuildTarget.iOS:
                    PlayerSettings.iOS.buildNumber = versionCode;
                    break;
            }
        }
    }
}
