using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using EonaCat_WindowsKeyViewer.Models;
using EonaCat_WindowsKeyViewer.Models.Objects;

namespace EonaCat_WindowsKeyViewer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly WindowsModel _windowsModel = new WindowsModel();

        public MainWindow()
        {
            InitializeComponent();

        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            _windowsModel.GetWindowsInformation();

            if (_windowsModel.InformationRetrieved)
            {
                foreach (WindowsInformation windows in _windowsModel.WindowsList)
                {
                    windowsNameLabel.Content = windows.Caption;
                    windowsCountryCodeLabel.Text = "Country: " + windows.CountryCode;
                    windowsVersionLabel.Text = "Version : " + windows.Version;
                    windowsCSDVersionLabel.Text = "CSD Version : " + windows.CSDVersion;
                    InstallationDateLabel.Text = "Installation date : " + windows.InstallDate;
                    windowsSerialNumberLabel.Text = "Serialnumber : " + windows.SerialNumber;
                    registeredOwnerLabel.Text = "Registered User : " + windows.RegisteredUser;
                    organisationLabel.Text = "Organization : " + windows.Organization;
                    windowsBuildNumberLabel.Text = "Build: " + windows.BuildNumber;
                }
            }
        }

        private void checkProductKeyButton_Click(object sender, RoutedEventArgs e)
        {
            var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            var reg = baseKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);

            if (reg != null)
            {
                var digitalProductId = reg.GetValue("DigitalProductId") as byte[];
                if (digitalProductId != null)
                {
                    productKeyTextBox.Text = _windowsModel.DecodeProductKey(digitalProductId);
                }
                else
                {
                    MessageBox.Show("Information could not be retrieved, are you running the EonaCat WindowsKeyViewer as an admin?");
                }
            }
        }
    }
}
