using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using TodoList.ViewModels;

namespace TodoList.Views
{
    public partial class MainWindow : Window
    {
        public MainWindow(MainViewModel viewModel)
        {
            InitializeComponent();
            DataContext = viewModel;
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
            // Verify if app shuts down? No, ShutdownMode is Explicit.
        }

        private void Window_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                // If settings are open, close settings? 
                // But user requirement is "Equals pressing X button", which usually means Close/Hide window.
                // However, if we want better UX:
                if (DataContext is MainViewModel { IsSettingsOpen: true } vm)
                {
                    vm.IsSettingsOpen = false;
                    e.Handled = true;
                    return;
                }

                // Default behavior: Close (Hide) Window
                Close();
            }
        }

        private void ShortcutBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            e.Handled = true;
            
            // Ignore modifier keys alone being the "main" key
            if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl ||
                e.Key == Key.LeftAlt || e.Key == Key.RightAlt ||
                e.Key == Key.LeftShift || e.Key == Key.RightShift ||
                e.Key == Key.LWin || e.Key == Key.RWin)
            {
                return;
            }

            var key = e.Key;
            if (key == Key.System) key = e.SystemKey; // Handle Alt+Key

            // Build modifier string
            var modifiers = new System.Collections.Generic.List<string>();
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) modifiers.Add("Control");
            if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt) modifiers.Add("Alt");
            if ((Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift) modifiers.Add("Shift");
            if ((Keyboard.Modifiers & ModifierKeys.Windows) == ModifierKeys.Windows) modifiers.Add("Windows");

            // Map key to string
            string keyStr = key.ToString();
            
            // Simple mapping for letters/digits (A-Z, 0-9)
            if (keyStr.Length == 2 && keyStr.StartsWith("D") && char.IsDigit(keyStr[1]))
            {
                keyStr = keyStr.Substring(1);
            }

            // Update ViewModel
            if (DataContext is MainViewModel vm)
            {
                vm.ShortcutModifiers = string.Join(",", modifiers);
                vm.ShortcutKey = keyStr;
            }
        }

        private void ListBoxItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender is ListBoxItem item && item.DataContext is Models.TodoItem todoItem && DataContext is MainViewModel vm)
            {
                vm.OpenEditDialogCommand.Execute(todoItem);
            }
        }

        private void Hyperlink_RequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) { UseShellExecute = true });
        }
    }
}
