using GenericWpfApp.Model;
using GenericWpfApp.View;
using GenericWpfApp.ViewModel;
using NLog;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace GenericWpfApp
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
      private ILogger _logger;

      public MainWindowViewModel _mainWindowViewModel;

      public ImpedanceCheckWindow _impedanceCheckWindow;

      public MainWindow()
      {

         InitializeComponent();

         _logger = LogManager.GetCurrentClassLogger();

         string version = Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion; //0.1.0

         this.txtBlockAppTitle.Text += $" [Version: {version}]";

         _mainWindowViewModel = new MainWindowViewModel();
         DataContext = _mainWindowViewModel;

         PassthroughData._s_datagridColumnCount = datagridPassthroughData.Columns.Count;

         WinProc.Instance(this);
      }

      private void btnClose_Click(object sender, RoutedEventArgs e)
      {
         this.Close();
      }

      private void btnMax_Click(object sender, RoutedEventArgs e)
      {
         if (this.WindowState == WindowState.Maximized)
         {
            this.WindowState = WindowState.Normal;
            imgMax.Source = new BitmapImage(new System.Uri("pack://application:,,,/Resources/Images/Title_Bar_Buttons/maximize.png"));
         }
         else
         {
            this.WindowState = WindowState.Maximized;
            imgMax.Source = new BitmapImage(new System.Uri("pack://application:,,,/Resources/Images/Title_Bar_Buttons/restore.png"));
         }
      }

      private void btnMin_Click(object sender, RoutedEventArgs e)
      {
         this.WindowState = WindowState.Minimized;
      }

      private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
      {
         DragMove();
      }

      private void imgAppIcon_MouseDown(object sender, MouseButtonEventArgs e)
      {
         // simulate ALT+SPACE press
         CommonLib.Runtime.InteropServices.Win32Helpers.Simulate_2_Key_Press(CommonLib.Runtime.InteropServices.Win32Helpers.VK.MENU, (char)CommonLib.Runtime.InteropServices.Win32Helpers.VK.SPACE);
      }

      private void btnPower_Click(object sender, RoutedEventArgs e)
      {
         _impedanceCheckWindow = new ImpedanceCheckWindow();
         _impedanceCheckWindow.ShowDialog();

         WinProc.Instance().StartPowerSupplyReadTask();
      }
   }
}
