using Furion.DistributedIDGenerator;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Nirvana.Common;
using System;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using YBDevice.NApi.Application.BusinessClient.AccountInfo;

namespace YBDevice.NApi.Controllers
{
    /// <summary>
    /// 测试专用
    /// </summary>
    public class TestController : Controller
    {
        private readonly IDeviceService _deviceService;
        private readonly IAccountService _accountService;
        private readonly IDistributedCache _cache;
        private readonly IHttpContextAccessor _httpContextAccessor;
        public TestController(IDeviceService deviceService,IAccountService accountService, IDistributedCache cache, IHttpContextAccessor httpContextAccessor)
        {
            _deviceService = deviceService;
            _accountService = accountService;
            _cache = cache;
            _httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 模拟设备测量
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> IndexAsync()
        {
            var devicelist = await _deviceService.GetListAsync();
            return View(devicelist);
        }

        public IActionResult testid()
        {
            var id = IDGen.NextID();
            return Content(id.ToString());
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public FileResult qr(string param)
        {
            var map = BarcodeHelper.QrCode(param, 230, 230);
            MemoryStream ms = new MemoryStream();
            map.Save(ms, ImageFormat.Jpeg);
            return File(ms.ToArray(), "image/jpg");
        }

        public async Task<ActionResult> aaAsync()
        {
            string a = "2342341";
            byte[] aas = Encoding.UTF8.GetBytes(a);
            await _cache.SetAsync("YBCupCache:Container:testkey", aas, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(60)));
            await _cache.SetAsync("YBCupCache:Container:testkey1", aas, new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(60)));
            return Content("3");
        }

        /// <summary>
        /// YB_Result迁移到YB_nResult
        /// </summary>
        public ActionResult Result2NewResult()
        {
            _deviceService.Result2NewResult();
            return Content("333");
        }

        public IActionResult testurl(string a="123")
        {
            TempData["test"] = a;
            return Redirect("testurl1");
        }

        public IActionResult testurl1()
        {
            var b = TempData["test"];
            return Content($"返回值:{b.ToString()}");
        }

        public async Task<ActionResult> TestRedisAsync()
        {
            var sn = "123";
            var key = $"YBDeviceCache:Container:{sn}";
            string param1 = "122132312313";
            var param1byte = Encoding.UTF8.GetBytes(param1);
            var options = new DistributedCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(5));
            await _cache.SetAsync(key, param1byte, options);
            return Content("3");
        }
    }
}
