using UnityEditor;
using UnityEngine;
using Terra.Arbitrator.Settings;

namespace Terra.Arbitrator.GUI
{
    internal class BetterGitSettingsProvider : SettingsProvider
    {
        // The path in the Project Settings window.
        private const string SettingsPath = "Project/Better Git";

        // Constructor
        private BetterGitSettingsProvider(string path, SettingsScope scope = SettingsScope.User)
            : base(path, scope) {}

        /// <summary>
        /// This static method is what registers our settings provider with Unity.
        /// </summary>
        [SettingsProvider]
        public static SettingsProvider CreateSettingsProvider()
        {
            var provider = new BetterGitSettingsProvider(SettingsPath, SettingsScope.Project);
            return provider;
        }

        /// <summary>
        /// This is where we draw the UI for our settings page.
        /// </summary>
        public override void OnGUI(string searchContext)
        {
            // Load the current settings directly within OnGUI.
            var username = BetterGitSettings.Username;
            var email = BetterGitSettings.Email;
            
            EditorGUILayout.Space();

            EditorGUILayout.LabelField("Git Credentials", EditorStyles.boldLabel);
            EditorGUILayout.HelpBox("These credentials will be used for Git operations like Push. They are saved per-user and shared across your projects. Changes are saved automatically.", MessageType.Info);
            
            EditorGUILayout.Space();

            
            EditorGUI.BeginChangeCheck();
            username = EditorGUILayout.TextField("Username", username);
            email = EditorGUILayout.TextField("Email Address", email);
            if (!EditorGUI.EndChangeCheck()) return;
            
            BetterGitSettings.Username = username;
            BetterGitSettings.Email = email;
        }
    }
}