#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;

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

            var sdkEnvironmentProp = serializedObject.FindProperty("sdkEnvironment");
            var appIdProp = serializedObject.FindProperty("appId");

            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"));
                EditorGUILayout.HelpBox(
                    sdkEnvironmentProp.enumValueIndex == (int)global::IGP.UnitySDK.IGPSDKEnvironment.DEV
                        ? "DEV 会连接 IndieSpark 桌面端，仅用于开发测试。"
                        : "PROD 会连接 IndieGamesPass 桌面端，正式发布请保持该模式。",
                    MessageType.None);
            }

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

            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
