using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace Wetterstation
{
    public partial class Settings : Form
    {
        public Settings()
        {
            InitializeComponent();
            lblFrontendId.Text = "Frontend Id: " + generateId();
        }

        private void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }

        private String generateId()
        {
            String result = "Could not found id";
            
            try {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Processor");

                foreach (ManagementObject queryObj in searcher.Get()) {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_Processor instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Architecture: {0}", queryObj["Architecture"]);
                    Console.WriteLine("Caption: {0}", queryObj["Caption"]);
                    Console.WriteLine("Family: {0}", queryObj["Family"]);
                    Console.WriteLine("ProcessorId: {0}", queryObj["ProcessorId"]);
                    Console.WriteLine("Username: {0}", Environment.UserName);
                    result = queryObj["ProcessorId"] + Environment.UserName;
                }
            }
            catch (ManagementException e) {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
            using (MD5 md5Hash = MD5.Create())
            {
                return GetMd5Hash(md5Hash, result);
            }
        }
        private string GetMd5Hash(MD5 md5Hash, string input)
        {

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }

        private void Settings_Load(object sender, EventArgs e)
        {

        }
    }
}