using Insight.Remote.Windows.ViewModels;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using Button = System.Windows.Controls.Button;
using MessageBox = System.Windows.MessageBox;

namespace Insight.Remote.Windows.Views;

public partial class MainWindow : Window
{
    public MainViewModel? ViewModel => DataContext as MainViewModel;

    public MainWindow()
    {
        InitializeComponent();
    }

    public MainWindow(MainViewModel viewModel)
    {
        DataContext = viewModel;
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        if (DesignerProperties.GetIsInDesignMode(this)) return;
        if (DataContext is not MainViewModel vm) return;
    }

    private void Window_Closing(object sender, CancelEventArgs e)
    {
        if (DataContext is not MainViewModel vm) return;
        vm.Shutdown();
    }

    private void OptionsButton_Click(object sender, RoutedEventArgs e)
    {
        if (sender is not Button button) return;
        button.ContextMenu.IsOpen = true;
    }

    private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DragMove();
    }

    private void ElevateToAdminMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (DataContext is not MainViewModel vm) return;

        if (vm.ElevateToAdminCommand.CanExecute(null)) vm.ElevateToAdminCommand.Execute(null);
        else MessageBox.Show("Unable to execute in current state.", "Unable to Execute", MessageBoxButton.OK, MessageBoxImage.Warning);
    }

    private void ElevateToServiceMenuItem_Click(object sender, RoutedEventArgs e)
    {
        if (DataContext is not MainViewModel vm) return;

        if (vm.ElevateToServiceCommand.CanExecute(null)) vm.ElevateToServiceCommand.Execute(null);
        else MessageBox.Show("Unable to execute in current state.", "Unable to Execute", MessageBoxButton.OK, MessageBoxImage.Warning);
    }
}