using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

namespace EngineSdkConverter.Unity.Editor
{
    public class TYPostProcessiOS
    {
        const string TAG = "TYPostProcessiOS OnPostProcessBuild ";
        const string ENGINE_SDK_CONVERTER_RESOURCE_FROM_UNITY_RELATIVE_PATH = "EngineSdkConverter/Unity/Editor/iOS";

        const string ENGINE_SDK_CONVERTER_PLUGINS_FROM_UNITY_RELATIVE_PATH = "EngineSdkConverter/Unity/Plugins/iOS";
        const string ENGINE_SDK_CONVERTER_PYTHON_PACKAGE_SITE_PATH = "EngineSdkConverter/Unity/Plugins/iOS/python_sites";
        const string GAME_BUILD_CONFIG_FILE_PATH = "gameConfigs";
        const string GAME_BUILD_CONFIG_FILE_NAME = "gameconfig.json";

        const string UNION_SPECIAL_LOGO = "union_special_logo.png";

        // iOSSDK资源-脚本文件名称
        const string CUSTOMIZE_SCRIPT_FILE_NAME = "EngineSdkiOSBuildProcess.py";

        // 项目Unity Traget Name
        const string UNITY_TARGET_NAME = "Unity-iPhone";
        const string UNITY_FRAMEWORK_TARGET_NAME = "UnityFramework";

        [PostProcessBuild(49)]
        public static void OnPostProcessBuild(BuildTarget buildTarget, string path)
        {
            Debug.Log(TAG + " start " + path);

            if (buildTarget == BuildTarget.iOS)
            {
                Debug.Log(TAG + "start");
                // 移动脚本/配置 - 运行脚本
                string resourceOfUnityPluginsPath = Path.Combine(Application.dataPath, ENGINE_SDK_CONVERTER_RESOURCE_FROM_UNITY_RELATIVE_PATH);
                string pythonPackageSitePath = Path.Combine(Application.dataPath, ENGINE_SDK_CONVERTER_PYTHON_PACKAGE_SITE_PATH);
                string pluginsPath = Path.Combine(Application.dataPath, ENGINE_SDK_CONVERTER_PLUGINS_FROM_UNITY_RELATIVE_PATH);
                
                string unionSpecialLogoPath = Path.Combine(pluginsPath, UNION_SPECIAL_LOGO);
                string targetUnionSpecialLogoPath = Path.Combine(path, UNION_SPECIAL_LOGO);
                File.Copy(unionSpecialLogoPath, targetUnionSpecialLogoPath, true);

                string customScriptPath = Path.Combine(resourceOfUnityPluginsPath, CUSTOMIZE_SCRIPT_FILE_NAME);
                string targetScriptPath = Path.Combine(path, CUSTOMIZE_SCRIPT_FILE_NAME);
                File.Copy(customScriptPath, targetScriptPath, true);
                // 资源
                string customGameConfigPath = Path.Combine(resourceOfUnityPluginsPath, GAME_BUILD_CONFIG_FILE_PATH);
                string targetGameConfigPath = path;
                EngineSdkConverterUtils.CopyDirectory(customGameConfigPath, targetGameConfigPath, true);
                EngineSdkConverterUtils.CopyDirectory(pythonPackageSitePath, targetGameConfigPath, true);
                string targetConfigPath = Path.Combine(path, GAME_BUILD_CONFIG_FILE_NAME);
                TryRunBuildScript(path, targetScriptPath, targetConfigPath);
            }
        }

        static void TryRunBuildScript(string workspace, string scriptFilePath, string configFilePath)
        {
            Debug.Log(TAG + " TryRunBuildScript start " + workspace + " " + scriptFilePath);
            string iosMinOSVersion = PlayerSettings.iOS.targetOSVersionString;
            string arguments = $"--configFile \"{configFilePath}\" --workspace \"{workspace}\" --minos \"{iosMinOSVersion}\" --targetName \"{UNITY_FRAMEWORK_TARGET_NAME}\"";
            Debug.Log(TAG + $"EngineSDKConverter RunPython  arguments :{arguments} ");
            Result buildResult = EngineSdkConverterUtils.RunPython(scriptFilePath, arguments, workspace);
            Debug.Log(TAG + $"EngineSDKConverter buildResult error :{buildResult.ExitCode} ");
            Debug.Log(TAG + $"EngineSDKConverter buildResult error :{buildResult.Message} ");
            Debug.Log(TAG + "TryRunBuildScript finish");
            if (buildResult.ExitCode != 0)
            {
                if (Application.isBatchMode)
                {
                    EditorApplication.Exit(-1);
                }
                else
                {
                    throw new System.Exception("EngineSDKConverter build failed: " + buildResult.Message);
                }
            }
        }
    }
}