using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using StackExchange.Profiling;
using WIDESEAWCS_Core;
using WIDESEAWCS_Core.Authorization;
using WIDESEAWCS_Core.BaseController;
using WIDESEAWCS_Core.Const;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.HttpContextUser;
using WIDESEAWCS_Core.Utilities;
using WIDESEAWCS_ISystemServices;
using WIDESEAWCS_Model;
using WIDESEAWCS_Model.Models;

namespace WIDESEAWCS_WCSServer.Controllers
{
    [Route("api/User")]
    [ApiController]
    public class Sys_UserController : ApiBaseController<ISys_UserService, Sys_User>
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public Sys_UserController(ISys_UserService userService, IHttpContextAccessor httpContextAccessor) : base(userService)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        [HttpPost, Route("login"), AllowAnonymous]
        public IActionResult Login([FromBody] LoginInfo loginInfo)
        {
            return Json(Service.Login(loginInfo));
        }
        [HttpPost, Route("getCurrentUserInfo")]
        public WebResponseContent GetCurrentUser()
        {
            return Service.GetCurrentUserInfo();
        }

        [HttpPost, Route("modifyPwd")]
        public IActionResult ModifyPwd(string oldPwd, string newPwd)
        {
            return Json(Service.ModifyPwd(oldPwd, newPwd));
        }

        [HttpGet, Route("getVierificationCode"), AllowAnonymous]
        public IActionResult GetVierificationCode()
        {
            //var html = MiniProfiler.Current.RenderIncludes(_httpContextAccessor.HttpContext);
            string code = "1234" /*VierificationCode.RandomText()*/;
            var data = new
            {
                img = VierificationCode.CreateBase64Imgage(code),
                uuid = Guid.NewGuid()
            };

            return Json(data);
        }
        [HttpGet, Route("SerializeJwt"), AllowAnonymous]
        public WebResponseContent SerializeJwt(string code)
        {
            return WebResponseContent.Instance.OK(data: JwtHelper.SerializeJwt(code));
        }
        [HttpPost, Route("replaceToken")]
        public WebResponseContent ReplaceToken()
        {
            WebResponseContent responseContent = new WebResponseContent();
            try
            {
                string token = App.User.GetToken();
                if (string.IsNullOrEmpty(token))
                {
                    return responseContent = WebResponseContent.Instance.Error("token无效,请重新登录!");
                }
                TokenModelJwt tokenModelJwt = new TokenModelJwt()
                {
                    RoleId = App.User.RoleId,
                    TenantId = App.User.TenantId,
                    UserId = App.User.UserId,
                    UserName = App.User.UserName
                };
                string newToken = JwtHelper.IssueJwt(tokenModelJwt);
                return responseContent = WebResponseContent.Instance.OK(data: newToken);
            }
            catch (Exception ex)
            {
                return responseContent.Error(ex.Message);
            }

        }
    }
}