// Copyright © 2017-2018 Atomic Software, LLC. All Rights Reserved.
// See LICENSE.md for full license information.

using Atom.Core.Game;
using Atom.Core.Networking.Messages;
using Atom.Core.Networking.Messages.Structures.User;
using Atom.LoginServer.Logic;
using Atom.LoginServer.Services;

namespace Atom.LoginServer.Networking.Messages.Handlers
{
    public static class UserHandlers
    {
        [MessageHandler(MessageProtocol.NC_USER_CLIENT_VERSION_CHECK_REQ)]
        public static void NC_USER_CLIENT_VERSION_CHECK_REQ(NetworkMessage msg)
        {
            var VersionKey = msg.ReadString(64);
            ClientLogic.CheckVersionKey(msg.Client, VersionKey);
        }

        [MessageHandler(MessageProtocol.NC_USER_US_LOGIN_REQ)]
        public static void NC_USER_US_LOGIN_REQ(NetworkMessage msg)
        {
            var Username = msg.ReadString(260);
            var Password = msg.ReadString(36);
            var SpawnApps = msg.ReadString(20);

            AccountLogic.Login(msg.Client, Username, Password, SpawnApps);
        }

        [MessageHandler(MessageProtocol.NC_USER_XTRAP_REQ)]
        public static void NC_USER_XTRAP_REQ(NetworkMessage msg)
        {
            var XTrapClientKeyLength = msg.ReadByte();
            var XTrapClientKey = msg.ReadBytes(XTrapClientKeyLength);

            ClientLogic.CheckXTrapKey(msg.Client, XTrapClientKey);
        }

        [MessageHandler(MessageProtocol.NC_USER_WORLD_STATUS_REQ)]
        public static void NC_USER_WORLD_STATUS_REQ(NetworkMessage msg)
        {
            new PROTO_NC_USER_WORLD_STATUS_ACK(WMService.Worlds).Send(msg.Client);
        }

        [MessageHandler(MessageProtocol.NC_USER_WORLDSELECT_REQ)]
        public static void NC_USER_WORLDSELECT_REQ(NetworkMessage msg)
        {
            var WorldNo = msg.ReadByte();
            WMLogic.SelectWorld(msg.Client, WorldNo);
        }

        [MessageHandler(MessageProtocol.NC_USER_NORMALLOGOUT_CMD)]
        public static void NC_USER_NORMALLOGOUT_CMD(NetworkMessage msg)
        {
            var LogoutType = (LogoutType) msg.ReadByte();
            ClientLogic.Logout(msg.Client, LogoutType);
        }

        [MessageHandler(MessageProtocol.NC_USER_LOGIN_WITH_OTP_REQ)]
        public static void NC_USER_LOGIN_WITH_OTP_REQ(NetworkMessage msg)
        {
            var OTP = msg.ReadString(32);
            AccountLogic.LoginWithOTP(msg.Client, OTP);
        }
    }
}
