using GxPress.Service.Interface.VerificationCode;
using GxPress.Repository.Interface.VerificationCode;
using System.Threading.Tasks;
using System;
using Datory;
using GxPress.Request.App.VerificationCode;

namespace GxPress.Service.Implement.VerificationCode
{
    public class VerificationCodeService : IVerificationCodeService
    {
        private readonly IVerificationCodeRepository verificationCodeRepository;
        public VerificationCodeService(IVerificationCodeRepository verificationCodeRepository)
        {
            this.verificationCodeRepository = verificationCodeRepository;
        }
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetCodeAsync(int userId)
        {
            var r1 = new Random();
            var code = r1.Next(100000, 999999);
            var entity = new Entity.VerificationCode.VerificationCode();
            entity.UserId = userId;
            entity.TypeId = 1;
            entity.ExpireTime = 5;
            entity.Code = code;
            entity.IsDisable = false;
            await verificationCodeRepository.InsertAsync(entity);
            return code.ToString();
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        public async Task<bool> ConfirmationCodeAsync(VerificationCodeRequest request)
        {
            var verificationCode = await verificationCodeRepository.GetAsync(Q.Where(nameof(Entity.VerificationCode.VerificationCode.Code), request.Code).Where(nameof(Entity.VerificationCode.VerificationCode.IsDisable), false).OrderByDesc(nameof(Entity.VerificationCode.VerificationCode.CreatedDate)));
            if (verificationCode == null)
                throw new Exception("不存在");
            var createdDate = Convert.ToDateTime(verificationCode.CreatedDate).AddMinutes(verificationCode.ExpireTime);
            if (createdDate < DateTime.Now)
            {
                throw new Exception("验证码过期");
            }
            verificationCode.IsDisable = true;
            await verificationCodeRepository.UpdateAsync(verificationCode);
            return true;
        }


        /// <summary>
        /// 持久连接验证码
        /// </summary>
        /// <returns></returns>
        public async Task<bool> LastingConfirmationCodeAsync(VerificationCodeRequest request)
        {
            var userVerificationCode = await verificationCodeRepository.GetAsync(Q.Where(nameof(Entity.VerificationCode.VerificationCode.Code), request.Code).OrderByDesc(nameof(Entity.VerificationCode.VerificationCode.CreatedDate)));
            if (userVerificationCode == null)
                return false;
            var newUserVerificationCode = await verificationCodeRepository.GetAsync(Q.Where(nameof(Entity.VerificationCode.VerificationCode.UserId), userVerificationCode.UserId).OrderByDesc(nameof(Entity.VerificationCode.VerificationCode.CreatedDate)));
            if (!userVerificationCode.Code.Equals(newUserVerificationCode.Code))
                return false;
            var createdDate = Convert.ToDateTime(userVerificationCode.CreatedDate).AddMinutes(userVerificationCode.ExpireTime);
            if (createdDate < DateTime.Now)
            {
                return false;
            }
            return true;
        }
    }
}