// Copyright 2018 RED Software, LLC. All Rights Reserved.

using IgniteEngine;
using System;

namespace GameLogServer.Services
{
    internal static class AccountLogService
	{
		internal static bool AddLogin(int userNo, byte worldNo, string ip)
		{
			using (var p_AccountLog_Add = new StoredProcedure("p_AccountLog_Add", Program.AccountLogODBC))
			{
				p_AccountLog_Add.AddParameter("nType", AccountLogType.ACCOUNT_LOGIN);
				p_AccountLog_Add.AddParameter("nUserNo", userNo);
				p_AccountLog_Add.AddParameter("nWorldNo", worldNo);
				p_AccountLog_Add.AddParameter("nPlayMin", DBNull.Value);
				p_AccountLog_Add.AddParameter("nErrorCode", DBNull.Value);
				p_AccountLog_Add.AddParameter("sIP", ip, 16);
				p_AccountLog_Add.AddParameter("sUserId", DBNull.Value);
				p_AccountLog_Add.AddParameter("sUserPassword", DBNull.Value);
				p_AccountLog_Add.AddOutput<long>("nLogNo");

				return p_AccountLog_Add.Run().GetOutput<long>("nLogNo") != 0;  
			}
		}

		internal static bool AddLoginFail(string userId, string userPassword, string ip, ushort errorCode)
		{
			using (var p_AccountLog_Add = new StoredProcedure("p_AccountLog_Add", Program.AccountLogODBC))
			{
				p_AccountLog_Add.AddParameter("nType", AccountLogType.ACCOUNT_LOGINFAIL);
				p_AccountLog_Add.AddParameter("nUserNo", DBNull.Value);
				p_AccountLog_Add.AddParameter("nWorldNo", DBNull.Value);
				p_AccountLog_Add.AddParameter("nPlayMin", DBNull.Value);
				p_AccountLog_Add.AddParameter("nErrorCode", errorCode);
				p_AccountLog_Add.AddParameter("sIP", ip, 16);
				p_AccountLog_Add.AddParameter("sUserId", userId, 256);
				p_AccountLog_Add.AddParameter("sUserPassword", userPassword, 32);
				p_AccountLog_Add.AddOutput<long>("nLogNo");

				return p_AccountLog_Add.Run().GetOutput<long>("nLogNo") != 0;
			}
		}

		internal static bool AddLogout(int userNo, byte worldNo, ushort playMin, ushort errorCode)
		{
			using (var p_AccountLog_Add = new StoredProcedure("p_AccountLog_Add", Program.AccountLogODBC))
			{
				p_AccountLog_Add.AddParameter("nType", AccountLogType.ACCOUNT_LOGOUT);
				p_AccountLog_Add.AddParameter("nUserNo", userNo);
				p_AccountLog_Add.AddParameter("nWorldNo", worldNo);
				p_AccountLog_Add.AddParameter("nPlayMin", playMin);
				p_AccountLog_Add.AddParameter("nErrorCode", errorCode);
				p_AccountLog_Add.AddParameter("sIP", DBNull.Value);
				p_AccountLog_Add.AddParameter("sUserId", DBNull.Value);
				p_AccountLog_Add.AddParameter("sUserPassword", DBNull.Value);
				p_AccountLog_Add.AddOutput<long>("nLogNo");

				return p_AccountLog_Add.Run().GetOutput<long>("nLogNo") != 0;
			}
		}
	}
}