#if MACCATALYST
using AppKit;
using Foundation;

namespace TodoList.Maui.Services.Platforms
{
    /// <summary>
    /// macOS 平台全局热键服务实现类
    /// 使用 AppKit 框架实现全局热键功能
    /// </summary>
    public class MacGlobalHotKeyService : IGlobalHotKeyService
    {
        private NSObject? _eventMonitor;
        private Action? _callback;
        private bool _isRegistered;
        private NSEventModifierMask _currentModifiers;
        private string _currentKey;

        /// <summary>
        /// macOS 平台支持全局热键
        /// </summary>
        public bool IsSupported => true;

        /// <summary>
        /// 注册全局热键
        /// </summary>
        public void RegisterHotKey(string modifiers, string key, Action callback)
        {
            _callback = callback;
            _currentModifiers = ParseModifiers(modifiers);
            _currentKey = key.ToUpper();

            if (_isRegistered)
            {
                UnregisterHotKey();
            }

            _eventMonitor = NSEvent.AddGlobalMonitorForEventsMatchingMask(
                NSEventMask.KeyDown,
                HandleKeyDown
            );

            _isRegistered = true;
        }

        /// <summary>
        /// 注销全局热键
        /// </summary>
        public void UnregisterHotKey()
        {
            if (_eventMonitor != null)
            {
                NSEvent.RemoveMonitor(_eventMonitor);
                _eventMonitor = null;
                _isRegistered = false;
            }
        }

        /// <summary>
        /// 更新热键配置
        /// </summary>
        public void UpdateHotKey(string modifiers, string key)
        {
            if (_callback != null)
            {
                RegisterHotKey(modifiers, key, _callback);
            }
        }

        /// <summary>
        /// 处理键盘按下事件
        /// </summary>
        private void HandleKeyDown(NSEvent evt)
        {
            bool modifiersMatch = false;

            if (_currentModifiers.HasFlag(AppKit.NSEventModifierMask.CommandKey) &&
                evt.ModifierFlags.HasFlag(AppKit.NSEventModifierMask.CommandKey))
            {
                modifiersMatch = true;
            }

            if (_currentModifiers.HasFlag(AppKit.NSEventModifierMask.AlternateKey) &&
                evt.ModifierFlags.HasFlag(AppKit.NSEventModifierMask.AlternateKey))
            {
                modifiersMatch = true;
            }

            if (_currentModifiers.HasFlag(AppKit.NSEventModifierMask.ControlKey) &&
                evt.ModifierFlags.HasFlag(AppKit.NSEventModifierMask.ControlKey))
            {
                modifiersMatch = true;
            }

            if (_currentModifiers.HasFlag(AppKit.NSEventModifierMask.ShiftKey) &&
                evt.ModifierFlags.HasFlag(AppKit.NSEventModifierMask.ShiftKey))
            {
                modifiersMatch = true;
            }

            if (modifiersMatch && evt.CharactersIgnoringModifiers == _currentKey)
            {
                _callback?.Invoke();
            }
        }

        /// <summary>
        /// 解析修饰键字符串
        /// </summary>
        private AppKit.NSEventModifierMask ParseModifiers(string modifiers)
        {
            AppKit.NSEventModifierMask mask = 0;

            if (string.IsNullOrEmpty(modifiers)) return mask;

            var parts = modifiers.Split(',');
            foreach (var part in parts)
            {
                var p = part.Trim();
                if (p.Equals("Command", StringComparison.OrdinalIgnoreCase) ||
                    p.Equals("Cmd", StringComparison.OrdinalIgnoreCase))
                {
                    mask |= AppKit.NSEventModifierMask.CommandKey;
                }
                if (p.Equals("Option", StringComparison.OrdinalIgnoreCase) ||
                    p.Equals("Alt", StringComparison.OrdinalIgnoreCase))
                {
                    mask |= AppKit.NSEventModifierMask.AlternateKey;
                }
                if (p.Equals("Control", StringComparison.OrdinalIgnoreCase))
                {
                    mask |= AppKit.NSEventModifierMask.ControlKey;
                }
                if (p.Equals("Shift", StringComparison.OrdinalIgnoreCase))
                {
                    mask |= AppKit.NSEventModifierMask.ShiftKey;
                }
            }
            return mask;
        }
    }
}
#endif