using System;
using System.Collections;
using System.Globalization;
using System.Management;
using System.Windows;
using EonaCat_WindowsKeyViewer.Models.Objects;

namespace EonaCat_WindowsKeyViewer.Models
{
    public class WindowsModel
    {
        private ArrayList _windowsCollection = new ArrayList();

        internal string DecodeProductKey(byte[] digitalProductId)
        {
            int num;
            char[] characterArray = new char[] 
            {
                'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R', 'T', 'V', 'W', 'X',
                'Y', '2', '3', '4', '6', '7', '8', '9'
             };

            char[] characterArray2 = new char[0x1d];
            ArrayList list = new ArrayList();

            for (num = 52; num <= 67; num++)
            {
                list.Add(digitalProductId[num]);
            }

            for (num = 0x1c; num >= 0; num--)
            {
                if (((num + 1) % 6) == 0)
                {
                    characterArray2[num] = '-';
                }
                else
                {
                    int index = 0;

                    for (var i = 14; i >= 0; i--)
                    {
                        int num4 = (index << 8) | ((byte)list[i]);
                        list[i] = (byte)(num4 / 24);
                        index = num4 % 24;
                        characterArray2[num] = characterArray[index];
                    }
                }
            }
            return new string(characterArray2);
        }

        public ArrayList WindowsList { get; set; } = new ArrayList();

        public bool InformationRetrieved { get; private set; }

        public void GetWindowsInformation()
        {
            WindowsList.Clear();

            InformationRetrieved = false;

            try
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");

                var windows = new WindowsInformation();

                foreach (var managementBaseObject in searcher.Get())
                {
                    var managementObject = (ManagementObject) managementBaseObject;
                    windows.Caption = managementObject["Caption"].ToString();
                    windows.CountryCode = managementObject["CountryCode"].ToString();
                    windows.BuildNumber = managementObject["BuildNumber"].ToString();

                    try
                    {
                        windows.CSDVersion = managementObject["CSDVersion"].ToString();
                    }
                    catch (NullReferenceException)
                    {
                        windows.CSDVersion = "Unknown, or no service pack installed";
                    }

                    windows.Description = managementObject["Description"].ToString();
                    windows.RegisteredUser = managementObject["RegisteredUser"].ToString();
                    windows.InstallDate = managementObject["InstallDate"].ToString();

                    DateTime time = ToDateTime(windows.InstallDate);
                    windows.InstallDate = time.AddTicks(-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Ticks).ToLocalTime().ToString(CultureInfo.InvariantCulture);

                    try
                    {
                        windows.Organization = managementObject["Organization"].ToString();
                    }
                    catch (NullReferenceException)
                    {
                        windows.Organization = "";
                    }

                    windows.SerialNumber = managementObject["SerialNumber"].ToString();
                    windows.Version = managementObject["Version"].ToString();
                }

                WindowsList.Add(windows);
                InformationRetrieved = true;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.ToString());
            }
        }

        private static DateTime ToDateTime(string currentDate)
        {
            int year = DateTime.Now.Year;
            int month = 1;
            int day = 1;
            int hour = 0;
            int minute = 0;
            int second = 0;
            int millisecond = 0;
            string result = string.Empty;

            if ((string.Empty == currentDate) || (currentDate == null))
            {
                return DateTime.MinValue;
            }

            if (currentDate.Length != 0x19)
            {
                return DateTime.MinValue;
            }
            result = currentDate.Substring(0, 4);

            if ("****" != result)
            {
                year = int.Parse(result);
            }

            result = currentDate.Substring(4, 2);

            if ("**" != result)
            {
                month = int.Parse(result);
            }

            result = currentDate.Substring(6, 2);

            if ("**" != result)
            {
                day = int.Parse(result);
            }

            result = currentDate.Substring(8, 2);

            if ("**" != result)
            {
                hour = int.Parse(result);
            }

            result = currentDate.Substring(10, 2);

            if ("**" != result)
            {
                minute = int.Parse(result);
            }

            result = currentDate.Substring(12, 2);

            if ("**" != result)
            {
                second = int.Parse(result);
            }

            result = currentDate.Substring(15, 3);

            if ("***" != result)
            {
                millisecond = int.Parse(result);
            }
            return new DateTime(year, month, day, hour, minute, second, millisecond);
        }
    }
}
