#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 showAuthorization;
        private bool showHostSession;

        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            Draw("sdkEnvironment", "SDK Environment");
            Draw("appId", "App Id");
            Draw("hostSessionPlatform", "Host Session Platform");
            Draw("debugLogging", "Log Level");

            var appId = serializedObject.FindProperty("appId");
            if (appId != null && appId.intValue <= 0)
            {
                EditorGUILayout.HelpBox("App Id is required.", MessageType.Error);
            }

            showAuthorization = EditorGUILayout.Foldout(showAuthorization, "Authorization Fallback", true);
            if (showAuthorization)
            {
                Draw("showAuthorizationFailureFallback", "Show Fallback");
                Draw("authorizationFailureFallbackTitle", "Title");
                Draw("authorizationFailureFallbackMessage", "Message");
                Draw("authorizationFailureFallbackHint", "Hint");
                Draw("authorizationFailureFallbackButtonText", "Retry Button");
                Draw("authorizationFailureFallbackQuitButtonText", "Quit Button");
            }

            showHostSession = EditorGUILayout.Foldout(showHostSession, "Host Session", true);
            if (showHostSession)
            {
                Draw("desktopSessionAutoAttach", "Auto Attach");
                var platform = serializedObject.FindProperty("hostSessionPlatform");
                if (platform == null ||
                    platform.enumValueIndex == (int)global::IGP.UnitySDK.IGPHostSessionPlatform.Desktop)
                {
                    Draw("desktopPipeEndpoint", "Pipe Endpoint");
                    Draw("desktopExecutablePathDebugOverride", "Executable Debug Override");
                    Draw("desktopLaunchCommand", "Launch Command");
                }
            }

            serializedObject.ApplyModifiedProperties();
        }

        private void Draw(string propertyName, string label)
        {
            var property = serializedObject.FindProperty(propertyName);
            if (property != null)
            {
                EditorGUILayout.PropertyField(property, new GUIContent(label));
            }
        }
    }
}
#endif
