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.Data.SqlClient;
using System.Management;

namespace Trial
{
    public partial class frmSplashScreen : Form
    {
        public frmSplashScreen()
        {
            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 pictureBox1_Click(object sender, EventArgs e)
        {

        }
 
        private void frmSplashScreen_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        int _time = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {

            if (_time != 5)
                _time += 1;
            else
            {
                timer1.Stop();
                SqlCommand sqlCmd = new SqlCommand();
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.Connection = cn;
                SqlDataReader myReader;
                sqlCmd.CommandText = "select * from tblAppReg where ID=@ID";
                sqlCmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1;
                cn.Open();
                myReader = sqlCmd.ExecuteReader();
                if(myReader.Read())
                {
                    AESAlgorithm aes = new Trial.AESAlgorithm();
                    string appReg = aes.DecryptText(myReader[1].ToString(), "qwertyuiop", "123456789");
                    myReader.Close();
                    cn.Close();

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

                    CheckRegister.ID = hardwareCode;

                    string serial = GenerateSerial(hardwareCode);
                    if(serial==appReg.Substring(0,appReg.Length-1))
                    {
                        CheckRegister.Serial = appReg;

                        int type = 0;
                        if (appReg.Substring(appReg.Length - 1, 1) == "1")
                            CheckRegister.Type = "Home";
                        else if (appReg.Substring(appReg.Length - 1, 1) == "2")
                            CheckRegister.Type = "Premium";
                        else if (appReg.Substring(appReg.Length - 1, 1) == "3")
                            CheckRegister.Type = "Professional";
                    }
                    else
                    {
                        CheckRegister.Serial = appReg;
                        CheckRegister.Type = "Trial";
                    }
                    frmMain main = new Trial.frmMain();
                    main.Show();
                    this.Close();
                }
            }
        }
    }
}
