using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.DrawingCore.Imaging;
using System.IO;
using Library;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace MySystem.Areas.Api.Controllers
{
    [Area("Api")]
    [Route("Api/[controller]/[action]")]
    public class PublicMethodController : BaseController
    {
        public PublicMethodController(IHttpContextAccessor accessor, ILogger<BaseController> logger, IOptions<Setting> setting) : base(accessor, logger, setting)
        {
        }
        #region 图片验证码
        public FileContentResult CheckCode()
        {
            string code = function.get_Random(4);
            var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 120, 40);
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Gif);
            function.WriteCookie(_accessor.HttpContext, "checkcode", code);
            return File(stream.ToArray(), "image/gif");
        }
        #endregion
        
        #region 图片验证码
        public FileContentResult PictureCode(string Tag = "")
        {
            string code = function.get_Random(4);
            var bitmap = VerifyCodeHelper.GetSingleObj().CreateBitmapByImgVerifyCode(code, 120, 40);
            MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, ImageFormat.Gif);
            RedisDbconn.Instance.Set(Tag, code);
            RedisDbconn.Instance.SetExpire(Tag, 600);
            return File(stream.ToArray(), "image/jpg");
        }
        #endregion



    }
}