

using Bogus.DataSets;

using CloudGaming.Code.Account.Contract;
using CloudGaming.DtoModel.Account;
using CloudGaming.DtoModel.Account.Login;

using HuanMeng.DotNetCore.Redis;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudGaming.Code.Account.Login
{
    /// <summary>
    /// 手机号登录
    /// </summary>
    /// <param name="loginParams"></param>
    /// <param name="cloudGamingBase"></param>
    public class PhoneUserLogin(PhoneLoginParams loginParams, CloudGamingBase cloudGamingBase) : IUserAccount
    {
        public async Task<int> LoginAsync()
        {
            if (string.IsNullOrEmpty(loginParams.PhoneNumber))
            {
                throw MessageBox.Show(ResponseCode.ParamError, "手机号不能为空");
            }
            if (string.IsNullOrEmpty(loginParams.VerificationCode))
            {
                throw MessageBox.Show(ResponseCode.ParamError, "验证码不能为空");
            }
            //判断是否是测试账号
            if (!loginParams.PhoneNumber.Contains("99999999") && loginParams.VerificationCode == "1112")
            {
                if (!PhoneNumberValidator.IsPhoneNumber(loginParams.PhoneNumber))
                {
                    throw MessageBox.Show(ResponseCode.PhoneNumberException, "手机号格式错误");
                }
                var verificationCodeRedis = await cloudGamingBase.RedisCache.StringGetAsync($"App:sms:{loginParams.PhoneNumber}");
                if (verificationCodeRedis.IsNullOrEmpty)
                {
                    throw MessageBox.Show(ResponseCode.ParamError, "验证码已过期，请重新发送！");
                }
                var verificationCode = (string)verificationCodeRedis;
                if (verificationCode != loginParams.VerificationCode)
                {
                    throw MessageBox.Show(ResponseCode.ParamError, "验证码错误！");
                }
                await cloudGamingBase.RedisCache.KeyDeleteAsync($"App:sms:{loginParams.PhoneNumber}");
            }

            var userAccount = await cloudGamingBase.Dao.DaoUser.Context.T_User_Phone_Account.AsNoTracking().Where(it => it.PhoneNum == loginParams.PhoneNumber && !it.IsLogout).FirstOrDefaultAsync();
            return userAccount?.UserId ?? 0;
        }

        /// <summary>
        /// 创建登录账号
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task CreateLoginAsync(T_User user)
        {

            T_User_Phone_Account t_User_Phone_Account = new T_User_Phone_Account()
            {
                UserId = user.Id,
                IsLogout = false,
                NikeName = user.NickName,
                PhoneNum = loginParams.PhoneNumber,
                UpdatedAt = DateTime.Now,
                CreatedAt = DateTime.Now,
            };
            await cloudGamingBase.Dao.DaoUser.Context.T_User_Phone_Account.AddAsync(t_User_Phone_Account);
        }

        /// <summary>
        /// 
        /// </summary>
        public int LastLoginType { get; set; } = 1;

        /// <summary>
        /// 返回登录信息
        /// </summary>
        /// <param name="userDataPropertyEnum"></param>
        /// <returns></returns>
        public string GetUserDataProperty(UserDataPropertyEnum userDataPropertyEnum)
        {
            if (userDataPropertyEnum == UserDataPropertyEnum.PhoneNum)
            {
                return loginParams.PhoneNumber;
            }
            return "";
        }

        /// <summary>
        /// 设备号
        /// </summary>
        public string DeviceNumber
        {
            get
            {
                if (loginParams.DeviceNumber.Length > 100)
                {
                    loginParams.DeviceNumber = loginParams.DeviceNumber.Substring(0, 100);
                }
                return loginParams.DeviceNumber;
            }
        }
    }
}
