namespace Catalyst_Launcher
{
    using Catalyst_Launcher.My;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data.SqlClient;
    using System.Security.Cryptography;
    using System.Text;

    public class sqlconnector
    {
        private SqlCommand sqlcmd;
        private SqlConnection sqlcon1;

        public sqlconnector()
        {
            SqlConnection connection = new SqlConnection {
                ConnectionString = "Server=24.93.232.212,49172;Database=Account;User=Server_Lugapha;Pwd=Beautiful!Q@W#E$R;"
            };
            this.sqlcon1 = connection;
        }

        public bool CheckConnection()
        {
            bool flag;
            try
            {
                this.sqlcon1.Open();
                this.sqlcon1.Close();
                flag = true;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                Interaction.MsgBox(exception.Message, MsgBoxStyle.ApplicationModal, null);
                flag = false;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public object checkuser(string user, string pass)
        {
            string cmdText = "SELECT * FROM tUser WHERE sUserID = '" + user + "' and sUserPW = '" + pass + "'";
            try
            {
                this.sqlcon1.Open();
                this.sqlcmd = new SqlCommand(cmdText, this.sqlcon1);
                SqlDataReader reader = this.sqlcmd.ExecuteReader();
                reader.Read();
                if (reader.HasRows)
                {
                    string str2 = Conversions.ToString(reader[0]);
                    this.sqlcon1.Close();
                    return str2;
                }
                MyProject.Forms.Launcher.Status2.Show();
                MyProject.Forms.Launcher.Status2.Text = "Username or Password are Incorrect";
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                Interaction.MsgBox("Sql ERROR!! " + exception.Message, MsgBoxStyle.ApplicationModal, null);
                ProjectData.ClearProjectError();
            }
            this.sqlcon1.Close();
            return 0;
        }

        public string getMd5Hash(string input)
        {
            byte[] buffer = MD5.Create().ComputeHash(Encoding.Default.GetBytes(input));
            StringBuilder builder = new StringBuilder();
            int num2 = buffer.Length - 1;
            for (int i = 0; i <= num2; i++)
            {
                builder.Append(buffer[i].ToString("x2"));
            }
            return builder.ToString();
        }

        public object GetToken(object Uid)
        {
            Random random = new Random();
            string input = Conversions.ToString(random.Next(1, 0x1869f));
            string right = this.getMd5Hash(input);
            if (Operators.ConditionalCompareObjectGreater(Uid, 0, false))
            {
                bool flag = false;
                while (!flag)
                {
                    string cmdText = "SELECT * FROM tTokens WHERE sToken = '" + right + "'";
                    try
                    {
                        this.sqlcon1.Open();
                        this.sqlcmd = new SqlCommand(cmdText, this.sqlcon1);
                        SqlDataReader reader = this.sqlcmd.ExecuteReader();
                        reader.Read();
                        if (reader.HasRows)
                        {
                            Interaction.MsgBox("!!!Internal Error!!!!", MsgBoxStyle.ApplicationModal, null);
                            input = Conversions.ToString(random.Next(1, 0x1869f));
                            right = this.getMd5Hash(input);
                            this.sqlcon1.Close();
                        }
                        else
                        {
                            this.sqlcon1.Close();
                            cmdText = Conversions.ToString(Operators.ConcatenateObject(Operators.ConcatenateObject("delete from tTokens where nEMID = '", Uid), "'"));
                            this.sqlcon1.Open();
                            this.sqlcmd = new SqlCommand(cmdText, this.sqlcon1);
                            this.sqlcmd.ExecuteReader().Read();
                            this.sqlcon1.Close();
                            cmdText = Conversions.ToString(Operators.ConcatenateObject(Operators.ConcatenateObject(Operators.ConcatenateObject(Operators.ConcatenateObject("Insert into tTokens (nEMID,sToken) Values('", Uid), "', '"), right), "')"));
                            try
                            {
                                this.sqlcon1.Open();
                                this.sqlcmd = new SqlCommand(cmdText, this.sqlcon1);
                                this.sqlcmd.ExecuteReader().Read();
                                MyProject.Forms.Launcher.Status2.Show();
                                MyProject.Forms.Launcher.Status2.Text = "Logging In...";
                            }
                            catch (Exception exception1)
                            {
                                ProjectData.SetProjectError(exception1);
                                Exception exception = exception1;
                                Interaction.MsgBox("Error Setting New Token" + exception.Message, MsgBoxStyle.ApplicationModal, null);
                                ProjectData.ClearProjectError();
                            }
                            flag = true;
                            this.sqlcon1.Close();
                        }
                    }
                    catch (Exception exception3)
                    {
                        ProjectData.SetProjectError(exception3);
                        Exception exception2 = exception3;
                        Interaction.MsgBox("Sql ERROR!! " + exception2.Message, MsgBoxStyle.ApplicationModal, null);
                        ProjectData.ClearProjectError();
                    }
                    this.sqlcon1.Close();
                }
                return right;
            }
            return null;
        }
    }
}

