using APT.Infrastructure.Api;
using APT.Infrastructure.Api.Redis;
using APT.Infrastructure.Core;
using APT.PF.WebApi.Models;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.DrawingCore.Imaging;
using System.IO;
namespace APT.UT.WebApiControllers.Api.UT
{

    /// <summary>
    /// 验证码
    /// </summary>
    [Route("api/UT/VerificationCode")]
    public partial class VerificationCodeController :CommonApiController
    {

        /// <summary>
        /// 数字验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("Number")]
        public FileContentResult NumberVerifyCode(string random)
        {
            string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.NumberVerifyCode);
            byte[] codeImage = VerifyCodeHelper.GetSingleObj().CreateByteByImgVerifyCode(code, 100, 40);
            //var redisManage = ServiceLocator.Instance.GetService<RedisManager>();
            CsRedisManager.StringSet(SessionKey.VERIFY_CODE + random, code,300);
            return File(codeImage, @"image/jpeg");
        }


        /// <summary>
        /// 手机验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("PhoneVerifyCode")]
        public JsonActionResult<bool> PhoneNumberVerifyCode(string uid,string random)
        {
            return SafeExecute<bool>(() =>
            {
                string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.NumberVerifyCode, 6);
                Dictionary<string, string> dicNCode = new Dictionary<string, string>();
                dicNCode.Add("time", DateTime.Now.AddSeconds(120).ToString());
                dicNCode.Add("code", code);
                //var redisManage = ServiceLocator.Instance.GetService<RedisManager>();
                CsRedisManager.StringSet(SessionKey.VERIFY_MESSAGE_CODE + uid, dicNCode);
                return true;
            });
        }

        /// <summary>
        /// 字母验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("Abc")]
        public FileContentResult AbcVerifyCode(string random)
        {
            string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.AbcVerifyCode);
            var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
            //var redisManage = ServiceLocator.Instance.GetService<RedisManager>();
            CsRedisManager.StringSet(SessionKey.VERIFY_CODE + random, code);
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Png);
            return File(stream.ToArray(), "image/png");
        }

        /// <summary>
        /// 混合验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("Mix")]
        public FileContentResult MixVerifyCode(string random)
        {
            string code = VerifyCodeHelper.GetSingleObj().CreateVerifyCode(VerifyCodeHelper.VerifyCodeType.MixVerifyCode);
            var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 100, 40);
            //var redisManage = ServiceLocator.Instance.GetService<RedisManager>();
            CsRedisManager.StringSet(SessionKey.VERIFY_CODE + random, code);
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Gif);
            return File(stream.ToArray(), "image/gif");
        }

    }
}
