using Microsoft.Extensions.DependencyInjection;
using TodoList.Maui.Services;
using TodoList.Maui.Views;
using TodoList.Maui.Models;

namespace TodoList.Maui;

public partial class App : Application
{
	private readonly IHotKeySettingsService _settingsService;
	private readonly IGlobalHotKeyService _hotKeyService;
	private readonly ISystemTrayService _trayService;
	private Window? _mainWindow;
	private Window? _quickEntryWindow;
	private bool _isHotkeyRegistered;

	public App(IServiceProvider serviceProvider)
	{
		InitializeComponent();

		_settingsService = serviceProvider.GetRequiredService<IHotKeySettingsService>();
		_hotKeyService = serviceProvider.GetRequiredService<IGlobalHotKeyService>();
		_trayService = serviceProvider.GetRequiredService<ISystemTrayService>();
	}

	protected override Window CreateWindow(IActivationState? activationState)
	{
		_mainWindow = new Window(new MainPage())
		{
			Width = 450,
			Height = 640
		};

		_mainWindow.Destroying += (s, e) =>
		{
		};

		_mainWindow.Created += (s, e) =>
		{
		};

		return _mainWindow;
	}

	private void OnHotKeyPressed()
	{
		MainThread.BeginInvokeOnMainThread(() =>
		{
			ShowQuickEntryWindow();
		});
	}

	private void ShowMainWindow()
	{
		if (_mainWindow != null)
		{
			_mainWindow.Dispatcher.Dispatch(() =>
			{
#if WINDOWS
				if (_mainWindow.Handler != null)
				{
					var platformWindow = _mainWindow.Handler.PlatformView as Microsoft.UI.Xaml.Window;
					platformWindow?.Activate();
				}
#else
				_mainWindow.Focus();
#endif
			});
		}
	}

	private void ExitApplication()
	{
		_trayService?.Dispose();
		Application.Current?.Quit();
	}

	private void RegisterHotkey()
	{
		if (_hotKeyService == null || _settingsService == null) return;

		var config = _settingsService.GetConfig();
		if (_hotKeyService.IsSupported && config.IsEnabled)
		{
			_hotKeyService.RegisterHotKey(config.Modifiers, config.Key, OnHotKeyPressed);
			_isHotkeyRegistered = true;

			config.PropertyChanged += (s, args) =>
			{
				if (args.PropertyName == nameof(HotKeyConfig.Modifiers) ||
					args.PropertyName == nameof(HotKeyConfig.Key) ||
					args.PropertyName == nameof(HotKeyConfig.IsEnabled))
				{
					if (config.IsEnabled && _hotKeyService.IsSupported)
					{
						_hotKeyService.UpdateHotKey(config.Modifiers, config.Key);
					}
					else
					{
						_hotKeyService.UnregisterHotKey();
						_isHotkeyRegistered = false;
					}
				}
			};
		}
	}

	private void ShowQuickEntryWindow()
	{
		if (_quickEntryWindow == null)
		{
			_quickEntryWindow = new Window(new QuickEntryPage(() => 
			{
				if (_quickEntryWindow != null)
				{
					Application.Current?.CloseWindow(_quickEntryWindow);
					_quickEntryWindow = null;
				}
			}))
			{
				Width = 400,
				Height = 300
			};
		}

		Application.Current?.OpenWindow(_quickEntryWindow);
#if WINDOWS
		if (_quickEntryWindow.Handler != null)
		{
			var platformWindow = _quickEntryWindow.Handler.PlatformView as Microsoft.UI.Xaml.Window;
			platformWindow?.Activate();
		}
#else
		_quickEntryWindow.Focus();
#endif
	}
}