namespace OubHackMS.Users
{
    using MySql.Data.MySqlClient;
    using OubHackMS.Core;
    using OubHackMS.Crypto;
    using OubHackMS.MySQL;
    using System;
    using System.Security.Cryptography;

    internal class User
    {
        private RijndaelManaged aes = new RijndaelManaged();
        public byte bBranch;
        public bool bIsBanned;
        public byte[] bIV;
        public byte[] bKey;
        public byte[] bPermIV;
        public byte[] bPermKey;
        public long lSessionID;
        public long lSteamID;
        private OubHackMS.MySQL.MySQL mysql;
        public string sBanAdmin = "";
        public string sBanReason = "";
        public string sIP = "";
        public string sPassword = "";
        public string sUsername = "";

        public User(OubHackMS parent)
        {
            object obj2 = parent.GetClass("MySQL");
            if (obj2 == null)
            {
                throw new Exception("MySQL class not found!");
            }
            this.mysql = (OubHackMS.MySQL.MySQL) obj2;
        }

        private byte[] generateAESDecIV()
        {
            this.aes.KeySize = 0x80;
            this.aes.GenerateIV();
            return this.aes.IV;
        }

        private byte[] generateAESDecKey()
        {
            this.aes.KeySize = 0x80;
            this.aes.GenerateKey();
            return this.aes.Key;
        }

        private long generateSessionID()
        {
            byte[] buffer = new byte[8];
            new Random().NextBytes(buffer);
            return Math.Abs(BitConverter.ToInt64(buffer, 0));
        }

        public bool setup(string sUsername, string sPassword)
        {
            MySqlDataReader reader = (MySqlDataReader) this.mysql.query("SELECT * FROM `oubhack`.`mybb_users` WHERE `username`='" + sUsername + "';");
            while (reader.Read())
            {
                string str = MD5L.sComputeHash(MD5L.sComputeHash(reader.GetString("salt")) + sPassword);
                this.sUsername = reader.GetString("username");
                this.sPassword = reader.GetString("password");
                if (str != this.sPassword)
                {
                    return false;
                }
                MySqlDataReader reader2 = (MySqlDataReader) this.mysql.query("SELECT * FROM `oubhack`.`mybb_banned` WHERE `uid`=" + reader.GetInt32("uid") + ";");
                while (reader2.Read())
                {
                    this.bIsBanned = true;
                    MySqlDataReader reader3 = (MySqlDataReader) this.mysql.query("SELECT `username` FROM `oubhack`.`mybb_users` WHERE `uid`=" + reader2.GetInt32("admin") + ";");
                    while (reader3.Read())
                    {
                        this.sBanAdmin = reader3.GetString("username");
                    }
                    this.sBanAdmin = reader2.GetString("admin");
                    this.sBanReason = reader2.GetString("reason");
                }
                this.lSessionID = this.generateSessionID();
                this.lSteamID = Convert.ToInt64(reader.GetString("steam_id_64"));
                this.bKey = this.generateAESDecKey();
                this.bIV = this.generateAESDecIV();
                this.bPermKey = MD5L.computeHash(this.sUsername);
                this.bPermIV = MD5L.computeHash(Convert.ToString(this.lSteamID));
                reader2.Close();
                reader.Close();
                return true;
            }
            reader.Close();
            return false;
        }

        public bool update(string key, object value)
        {
            try
            {
                this.mysql.execute(string.Concat(new object[] { "UPDATE `oubhack`.`mybb_users` SET `", key, "`=", value, " WHERE `username`='", this.sUsername, "';" }));
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
    }
}

