using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace XLAB2
{
    public partial class MainWindow : Window
    {
        private readonly MainWindowViewModel _viewModel;

        internal MainWindow(PsvDataService service)
        {
            InitializeComponent();
            _viewModel = new MainWindowViewModel(service, new DialogService(this));
            DataContext = _viewModel;
        }

        private void DocumentListItem_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var item = sender as ListBoxItem;
            if (item != null)
            {
                item.IsSelected = true;
                item.Focus();
            }
        }

        private void DocumentLineRow_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var row = sender as DataGridRow;
            if (row != null)
            {
                row.IsSelected = true;
                row.Focus();
            }
        }

        private void DocumentLineRow_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var row = sender as DataGridRow;
            if (row == null)
            {
                return;
            }

            row.IsSelected = true;
            row.Focus();
            _viewModel.TryEditVerificationFromDoubleClick(row.Item as PsvDocumentLine);
        }

        private void DocumentGroupRow_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            var row = sender as DataGridRow;
            if (row != null)
            {
                row.IsSelected = true;
                row.Focus();
            }
        }

        private void FrpdDirectoryMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var window = new FrpdDirectoryWindow();
            window.Owner = this;
            window.ShowDialog();
        }

        private void PrsnDirectoryMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var window = new PrsnDirectoryWindow();
            window.Owner = this;
            window.ShowDialog();
        }

        private void SpnmtpDirectoryMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var window = new SpnmtpDirectoryWindow();
            window.Owner = this;
            window.ShowDialog();
        }

        private void TypeSizeDirectoryMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var window = new TypeSizeDirectoryWindow();
            window.Owner = this;
            window.ShowDialog();
        }

        private void EkzDirectoryMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var window = new EkzDirectoryWindow();
            window.Owner = this;
            window.ShowDialog();
        }

        private void VerificationReportsMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var window = new VerificationReportsWindow();
            window.Owner = this;
            window.ShowDialog();
        }

        private void SpoiDirectoryMenuItem_Click(object sender, RoutedEventArgs e)
        {
            var window = new SpoiDirectoryWindow();
            window.Owner = this;
            window.ShowDialog();
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            await _viewModel.InitializeAsync();
        }
    }
}
