namespace OubHackMS.OConsole.ConsoleCommands
{
    using OubHackMS.Core;
    using OubHackMS.Crypto;
    using OubHackMS.OConsole;
    using OubHackMS.Users;
    using System;

    internal class User : CommandPackage
    {
        private UserDB userdb;

        public User(OubHackMS parent) : base(parent)
        {
            base.dCommands["user/add/"] = new Func<string, string[], bool>(this.commandAdd);
            base.dCommands["user/remove/"] = new Func<string, string[], bool>(this.commandRemove);
            base.dCommands["user/query/"] = new Func<string, string[], bool>(this.commandQuery);
            base.dCommands["user/info/"] = new Func<string, string[], bool>(this.commandInfo);
            this.userdb = (UserDB) parent.GetClass("UserDB");
        }

        private bool commandAdd(string sCommand, string[] sArgs)
        {
            if (sArgs.Length < 3)
            {
                Console.WriteLine("Usage : user add <name> <pass> <sid>");
            }
            else
            {
                try
                {
                    Login.addUser(sArgs[0], MD5L.sComputeHash(sArgs[1]), sArgs[2]);
                    Console.WriteLine("User '" + sArgs[0] + "' added!");
                }
                catch (Exception exception)
                {
                    Console.WriteLine("ERROR : " + exception.Message);
                }
            }
            return true;
        }

        private bool commandInfo(string sCommand, string[] sArgs)
        {
            if (sArgs.Length < 1)
            {
                Console.WriteLine("Usage : user info <name>");
            }
            else
            {
                OubHackMS.Users.User user = this.userdb.getByName(sArgs[0]);
                if (user != null)
                {
                    Console.WriteLine(" = === = " + user.sUsername + " = === = ");
                    Console.WriteLine("\tSteam ID : " + user.lSteamID);
                    Console.WriteLine("\tSession ID : " + user.lSessionID);
                    Console.WriteLine("\tIP : " + user.sIP);
                    string str = "";
                    string str2 = "";
                    string str3 = "";
                    string str4 = "";
                    for (int i = 0; i < user.bKey.Length; i++)
                    {
                        str = str + user.bKey[i].ToString("x") + " ";
                    }
                    for (int j = 0; j < user.bIV.Length; j++)
                    {
                        str2 = str2 + user.bIV[j].ToString("x") + " ";
                    }
                    for (int k = 0; k < user.bPermKey.Length; k++)
                    {
                        str3 = str3 + user.bPermKey[k].ToString("x") + " ";
                    }
                    for (int m = 0; m < user.bPermIV.Length; m++)
                    {
                        str4 = str4 + user.bPermIV[m].ToString("x") + " ";
                    }
                    Console.WriteLine("\tAES Key : " + str);
                    Console.WriteLine("\tAES IV : " + str2);
                    Console.WriteLine("\tAES Perm Key : " + str3);
                    Console.WriteLine("\tAES Perm IV : " + str4);
                    Console.WriteLine("\tMD5 password : " + user.sPassword);
                    if (user.bIsBanned)
                    {
                        Console.WriteLine("\tBan admin : " + user.sBanAdmin);
                        Console.WriteLine("\tBan reason : " + user.sBanReason);
                    }
                }
                else
                {
                    Console.WriteLine("User not found!");
                }
            }
            return true;
        }

        private bool commandQuery(string sCommand, string[] sArgs)
        {
            return true;
        }

        private bool commandRemove(string sCommand, string[] sArgs)
        {
            if (sArgs.Length < 1)
            {
                Console.WriteLine("Usage : user remove <name>");
            }
            else
            {
                Console.WriteLine("User '" + sArgs[0] + "' removed!");
                Login.removeUser(sArgs[0]);
            }
            return true;
        }
    }
}

