using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Data.SqlClient;

namespace Trial
{
    public partial class frmRegisterApp : Form
    {
        public frmRegisterApp()
        {
            InitializeComponent();
        }

        private string GetHardwarSerial()
        {
            string cpuSerial = string.Empty;
            string hardSerial = string.Empty;
            string mainBoardSerial = string.Empty;
            //----------------------

            ManagementClass mgmt = new ManagementClass("Win32_Processor");
            ManagementObjectCollection objcol = mgmt.GetInstances();
            foreach (ManagementObject obj in objcol)
            {
                if (obj.Properties["ProcessorId"] != null)
                    cpuSerial = obj.Properties["ProcessorId"].Value.ToString().Trim();
            }

            //---------------------

            ManagementObjectSearcher sercher = new ManagementObjectSearcher("select * from Win32_PhysicalMedia");
            foreach (ManagementObject wmi_Hd in sercher.Get())
            {
                if (wmi_Hd["SerialNumber"] != null)
                    hardSerial = wmi_Hd["SerialNumber"].ToString().Trim();
            }

            //---------------------

            ManagementObjectSearcher sercher2 = new ManagementObjectSearcher("select * from Win32_BaseBoard");
            foreach (ManagementObject wmi_Board in sercher2.Get())
            {
                if (wmi_Board["SerialNumber"] != null)
                    mainBoardSerial = wmi_Board["SerialNumber"].ToString().Trim();
            }

            //-----------

            string systemId = cpuSerial + hardSerial + mainBoardSerial;
            char[] arr = systemId.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);
        }


        SqlConnection cn = new SqlConnection("Data Source=.;Initial Catalog=dbTest1;Integrated Security=True");

        private string GenerateSerial(string hardwareCode)
        {
            string code = hardwareCode.Substring(1, 10);
            string serial = string.Empty;
            for (int i = 0; i < code.Length; i++)
            {
                char ch = char.Parse(code.Substring(i, 1));
                serial += ((int)ch).ToString() + hardwareCode.Substring(i, 1);
            }

            serial = serial.Substring(1, 10);

            string serial2 = string.Empty;
            for (int i = 0; i < serial.Length; i++)
            {
                char ch = char.Parse(serial.Substring(i, 1));
                serial2 += ((int)ch).ToString() + serial.Substring(i, 1);
            }

            return "SNIR" + serial2;
        }

        private void frmRegisterApp_Load(object sender, EventArgs e)
        {

            string hardwareCode = GetHardwarSerial();
            if (string.IsNullOrEmpty(hardwareCode) || hardwareCode.Length < 12)
                hardwareCode = "HTYH604562358695255KLDFG";
            txtSystemId.Text = hardwareCode;
        }

      

        private void button1_Click(object sender, EventArgs e)
        {
            string serial = GenerateSerial(txtSystemId.Text);
            AESAlgorithm aes = new Trial.AESAlgorithm();
            if (serial == txtSerial.Text.Substring(0, txtSerial.Text.Length - 1))
            {
                SqlCommand sqlCmd = new SqlCommand();
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.Connection = cn;
                sqlCmd.CommandText = "update tblAppReg set RegisterCode=@RegisterCode where ID=@ID";
                sqlCmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1;
                sqlCmd.Parameters.Add("@RegisterCode", SqlDbType.NVarChar).Value =aes.EncryptText(txtSerial.Text,"qwertyuiop","123456789");
                cn.Open();
                sqlCmd.ExecuteNonQuery();
                cn.Close();
                Application.Restart();
            }
            else
            {
                MessageBox.Show("سریال نامعتبر");
            }
        }
    }
}