using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.BaseInfo.Models;
using Hncore.Pass.BaseInfo.Request;
using Hncore.Pass.BaseInfo.Request.Manager;
using Hncore.Pass.BaseInfo.Request.User;
using Hncore.Pass.BaseInfo.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System;
using System.Threading.Tasks;
using System.Web;

namespace Hncore.Pass.BaseInfo.Controllers
{
    [ApiVersion("1.0")]
    [Route("api/baseinfo/v{version:apiVersion}/Manager/[action]")]
    public class ManagerController : HncoreControllerBase
    {
        private ManageService _manageService;
        private IConfiguration _Configuration;

        public ManagerController(ManageService manageService, IConfiguration configuration)
        {
            _manageService = manageService;
            _Configuration = configuration;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost, AllowAnonymous]
        public async Task<ApiResult> LogIn([FromBody] LoginRequest request)
        {
            return Success(await _manageService.Login(request));
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost, AllowAnonymous]
        public async Task<ApiResult> LogInTemp([FromBody] LoginRequest request)
        {
            return Success(await _manageService.LoginTemp(request));
        }

        /// <summary>
        /// 登录 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost, AllowAnonymous]
        public async Task<ApiResult> LoginAndroidTemp([FromBody] LoginRequest request)
        {
            return Success(await _manageService.LoginAndroidTemp(request));
        }

        /// <summary>
        /// 图片验证码
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        public async Task<ApiResult> GetValidateCode()
        {         
            var code = "1234";// vCode.MakeCode(4);

            var key = "validate:" + DateTime.Now.ToString("yyyyMMddHHmmss") + "-" + Guid.NewGuid().ToString("N");

            if (!EnvironmentVariableHelper.IsAspNetCoreProduction)
            {
                LogHelper.Debug("登录验证码", $"code:{code},key:{key}");
            }
            var expireSeconds = 600;
           // await RedisHelper.SetAsync(key, code, expireSeconds);

            var img = ValidateCodeHelper.GenerateCodeImg(code);
            var data = new
            {
                key = key,
                img = img,
                expireSeconds = expireSeconds,
            };
            return Success(data);
        }

        /// <summary>
        /// 刷新token
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<ApiResult> AgainGetToken(RequestBase<AgainTokenDTO> request)
        {
            request.TenantId = Request.GetManageUserInfo().TenantId;
            request.OperaterId = Request.GetManageUserInfo().OperaterId;
            return Success(await _manageService.AgainGetToken(request),"");
        }
    }
}