namespace OubHackMS.Servers.Handlers
{
    using OubHackMS.Core;
    using OubHackMS.Logging;
    using OubHackMS.Servers;
    using OubHackMS.Users;
    using System;
    using System.IO;
    using System.Net.Sockets;

    internal class PacketLogin : PacketHandler
    {
        private UserDB userdb;

        public PacketLogin(OubHackMS parent) : base(parent)
        {
            base.sIndex = 1;
            this.userdb = (UserDB) base.parent.GetClass("UserDB");
        }

        public override void doWork(NetworkStream str)
        {
            BinaryReader reader = new BinaryReader(str);
            BinaryWriter writer = new BinaryWriter(str);
            string sUsername = reader.ReadString();
            string sPassword = reader.ReadString();
            byte num = reader.ReadByte();
            if (this.userdb.doLogin(sUsername, sPassword))
            {
                User user = this.userdb.getByName(sUsername);
                if (user.bIsBanned)
                {
                    writer.Write(false);
                    writer.Write("Banned by " + user.sBanAdmin + " for : '" + user.sBanReason + "'.");
                    this.userdb.doLogout(user);
                }
                else
                {
                    writer.Write(true);
                    user.sIP = "lol";
                    user.bBranch = num;
                    writer.Write(user.bKey.Length);
                    writer.Write(user.bKey, 0, user.bKey.Length);
                    writer.Write(user.bIV.Length);
                    writer.Write(user.bIV, 0, user.bIV.Length);
                    writer.Write(user.lSessionID);
                    writer.Write(user.lSteamID);
                    base.parent.Log(Level.USER_LOG_FILE, new object[] { user.sUsername, "Logged in." });
                }
            }
            else
            {
                writer.Write(false);
                writer.Write("Bad username or password.");
            }
        }
    }
}

