using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using GxPress.Service.Interface.VerificationCode;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Request.App.VerificationCode;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// 收藏上传验证码
    /// </summary>
    [Route("api/app/verification-code")]
    [ApiController]
    [Authorize]
    public partial class VerificationCodeController : ControllerBase
    {
        private readonly IVerificationCodeService verificationCodeService;

        private readonly ILoginContext _loginContext;
        public VerificationCodeController(IVerificationCodeService verificationCodeService, ILoginContext loginContext)
        {
            this.verificationCodeService = verificationCodeService;
            _loginContext = loginContext;
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        [HttpPost("send-code")]
        public async Task<string> GetCodeAsync()
        {
            return await verificationCodeService.GetCodeAsync(_loginContext.AccountId);
        }

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <returns></returns>
        [HttpPost("app-send-code")]
        public async Task<VerificationDto> GetAppCodeAsync()
        {
            var code = await verificationCodeService.GetCodeAsync(_loginContext.AccountId);
            var result = new VerificationDto();
            result.Code = code;
            var serviceUrl = GxPress.Common.Tools.ConfigHelper.GetValue("ServiceAddress:AddressUrlDownload");
            result.ServiceUrl = $"{serviceUrl}/mobile/pcupload";
            return result;
        }
        /// <summary>
        ///验证验证码码
        /// </summary>
        /// <returns></returns>
        [HttpPost("confirmation-code")]
        [AllowAnonymous]
        public async Task<bool> ConfirmationCodeAsync(VerificationCodeRequest request)
        {
            //request.UserId=_loginContext.AccountId;
            return await verificationCodeService.ConfirmationCodeAsync(request);
        }
        /// <summary>
        /// 持久连接验证码
        /// </summary>
        /// <returns></returns>
        [HttpPost("Lasting-confirmation-code")]
        [AllowAnonymous]
        public async Task<bool> LastingConfirmationCode(VerificationCodeRequest request)
        {
            return await verificationCodeService.LastingConfirmationCodeAsync(request);
        }
    }
}