#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;

namespace IGP.UnitySDK.Editor
{
    [CustomEditor(typeof(global::IGP.UnitySDK.IGPConfig))]
    public sealed class IGPConfigEditor : UnityEditor.Editor
    {
        private bool showDebugSettings;
        private bool showAuthorizationFallbackSettings;

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            var sdkEnvironmentProp = serializedObject.FindProperty("sdkEnvironment");
            var appIdProp = serializedObject.FindProperty("appId");
            var debugLoggingProp = serializedObject.FindProperty("debugLogging");
            var showNetworkDiagnosticsProp = serializedObject.FindProperty("showNetworkDiagnostics");

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("IGP Config", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox("常规接入时只需要确认 SDK Environment 并填写 appId。SDK 不会自动初始化，游戏需要在启动流程中主动调用 IGPRuntimeManager.InitializeAsync()。其余配置默认隐藏，仅用于调试或特殊分发。", MessageType.Info);
            EditorGUILayout.Space();

            if (sdkEnvironmentProp != null)
            {
                EditorGUILayout.PropertyField(sdkEnvironmentProp, new GUIContent("SDK Environment"));
                var environmentHelpText = "PROD 会连接 IndieGamesPass 桌面端，正式发布请保持该模式。";
                if (sdkEnvironmentProp.enumValueIndex == (int)global::IGP.UnitySDK.IGPSDKEnvironment.DEV)
                {
                    environmentHelpText = "DEV 会连接 IndieSpark 桌面端，仅用于开发测试。";
                }
                else if (sdkEnvironmentProp.enumValueIndex == (int)global::IGP.UnitySDK.IGPSDKEnvironment.PREVIEW)
                {
                    environmentHelpText = "PREVIEW 会连接 IndieGamesPass Preview 桌面端，用于预览环境联调。";
                }

                EditorGUILayout.HelpBox(
                    environmentHelpText,
                    MessageType.None);
            }

            if (appIdProp != null)
            {
                EditorGUILayout.PropertyField(appIdProp, new GUIContent("App Id"));
                if (appIdProp.intValue <= 0)
                {
                    EditorGUILayout.HelpBox("appId 为必填项，发布或联调前需要填写有效值。", MessageType.Error);
                }
            }

            if (debugLoggingProp != null)
            {
                EditorGUILayout.Space();
                EditorGUILayout.LabelField("Debug", EditorStyles.boldLabel);
                EditorGUILayout.PropertyField(debugLoggingProp, new GUIContent("Debug Logging"));
                if (debugLoggingProp.intValue != (int)global::IGP.UnitySDK.IGPLogLevel.Off)
                {
                    EditorGUILayout.HelpBox(
                        "Debug Logging 使用最低等级语义：选择 Warning 时记录 Warning 和 Error；Debug 会包含 hosted session、KCP、P2P 和 Mirror Transport 的详细收发摘要。正式发布建议使用 Off。",
                        MessageType.Warning);
                }
            }

            if (showNetworkDiagnosticsProp != null)
            {
                EditorGUILayout.PropertyField(showNetworkDiagnosticsProp, new GUIContent("Show Network Diagnostics"));
                if (showNetworkDiagnosticsProp.boolValue)
                {
                    EditorGUILayout.HelpBox(
                        "Show Network Diagnostics 显示 SDK 自带的 OnGUI 网络诊断浮层，包含 KCP 健康状态、故障原因、RTT、队列和 UDP 统计，不改变 KCP 心跳或数据面行为。",
                        MessageType.None);
                }
            }

            EditorGUILayout.Space();
            showAuthorizationFallbackSettings = EditorGUILayout.Foldout(showAuthorizationFallbackSettings, "Authorization Fallback", true);
            if (showAuthorizationFallbackSettings)
            {
                DrawDebugProperty("showAuthorizationFailureFallback", "Show Authorization Failure Fallback");
                DrawDebugProperty("authorizationFailureFallbackTitle", "Authorization Failure Fallback Title");
                DrawDebugProperty("authorizationFailureFallbackMessage", "Authorization Failure Fallback Message");
                DrawDebugProperty("authorizationFailureFallbackHint", "Authorization Failure Fallback Hint");
                DrawDebugProperty("authorizationFailureFallbackButtonText", "Authorization Failure Fallback Button Text");
                DrawDebugProperty("authorizationFailureFallbackQuitButtonText", "Authorization Failure Fallback Quit Button Text");
            }

            EditorGUILayout.Space();
            showDebugSettings = EditorGUILayout.Foldout(showDebugSettings, "调试参数", true);
            if (showDebugSettings)
            {
                DrawDebugProperty("heartbeatInterval", "Heartbeat Interval");
                DrawDebugProperty("autoConnect", "Auto Connect");
                DrawDebugProperty("desktopSessionAutoAttach", "Desktop Session Auto Attach");
                DrawDebugProperty("desktopPipeEndpoint", "Desktop Pipe Endpoint");
                DrawDebugProperty("desktopExecutablePathDebugOverride", "Desktop Executable Path Debug Override");
                DrawDebugProperty("desktopLaunchCommand", "Desktop Launch Command");
            }

            serializedObject.ApplyModifiedProperties();
        }

        private void DrawDebugProperty(string propertyName, string label)
        {
            var property = serializedObject.FindProperty(propertyName);
            if (property == null)
            {
                return;
            }

            EditorGUILayout.PropertyField(property, new GUIContent(label));
        }
    }
}
#endif
