﻿namespace RestaurantManagementSystem
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using RMS.ViewModels;
    using WPFTabTip;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            TabTipAutomation.BindTo<TextBox>();
            TabTipAutomation.BindTo<PasswordBox>();
            TabTipAutomation.IgnoreHardwareKeyboard = HardwareKeyboardIgnoreOptions.IgnoreAll;
            InitializeComponent();

            this.WaiterOperationsControl.Pass.Focus();
        }

        private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
        {
            if(!this.WaiterOperationsControl.IsVisible) return;

            if (e.Key == Key.Tab)
            {
                e.Handled = true;
                if (this.WaiterOperationsControl.Pass.IsFocused && this.WaiterOperationsControl.TableNumber.IsEnabled)
                {
                    this.WaiterOperationsControl.TableNumber.Focus();
                }
                else if (this.WaiterOperationsControl.TableNumber.IsFocused && this.WaiterOperationsControl.ItemId.IsEnabled)
                {
                    this.WaiterOperationsControl.ItemId.Focus();
                }
                else if (this.WaiterOperationsControl.ItemId.IsFocused && this.WaiterOperationsControl.SearchBox.IsVisible && this.WaiterOperationsControl.SearchBox.IsEnabled)
                {
                    this.WaiterOperationsControl.SearchBox.Focus();
                }
                else
                {
                    this.WaiterOperationsControl.Pass.Focus();
                }
            }
            else if (e.Key == Key.Escape)
            {
                e.Handled = true;
                (this.WaiterOperationsControl.DataContext as CurrentStateVm)?.Reset(null);
            }
        }
        private void MainWindow_OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            this.FocusGrid.Focus();
        }

        private void ExitMenuEntry_OnClick(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}
