using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Nirvana.Common;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using YBDevice.Body.BodyFatHelper;
using YBDevice.Entity;
using YBDevice.NApi.Application.SubscriberInfo;

namespace YBDevice.NApi.Controllers.Api
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TestApiController : ControllerBase
    {
        private readonly IBodyFatHelperService _bodyFatHelperService;
        private readonly ISubscriberService _subscriberService;
        public TestApiController(IBodyFatHelperService bodyFatHelperService, ISubscriberService subscriberService)
        {
            _bodyFatHelperService = bodyFatHelperService;
            _subscriberService = subscriberService;
        }
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value1" };
        }
        /// <summary>
        /// 测试算法
        /// </summary>
        /// <param name="weight"></param>
        /// <param name="height"></param>
        /// <param name="age"></param>
        /// <param name="sex"></param>
        /// <param name="adc"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult testcalc(double weight, double height, int age, GenderType sex, int adc)
        {
            var data = _bodyFatHelperService.CalcBodyFat(weight, height, age, adc, sex);
            return Content(data.ToJson());
        }

        [HttpPost]
        public IActionResult testerror()
        {
            throw new Exception("异常测试");
        }

        [HttpGet]
        [Authorize]
        public ActionResult<IEnumerable<string>> Get2()
        {
            return new string[] { "value2", "value2" };
        }

        [HttpGet]
        public async Task testsubscr()
        {
            await _subscriberService.DeleteUserDataAsync(new Application.DeleteUserDataS2SDto
            {
                DevType = 11,
                FamilyId = 1723,
                ResultId = Guid.Parse("08d9c530-0ef2-4514-8ee2-4be3beb2f40d"),
                time = DateTime.Parse("2021-12-22 17:47:24.937"),
                UserId = 1539
            });
            //await _subscriberService.UpdateUserHeightDataAsync(new YBDevice.Entity.UpdateUserHeightDataS2SDto
            //{
            //    Height=128,
            //    familyid=1716,
            //    DevType=14,
            //    LastHeight=5.2m,
            //    LastResultHeightTime=DateTime.Parse("2021-11-11 14:21:21"),
            //    HalfYearHeight=0,
            //    YearHeight=0
            //});
        }

        [HttpGet]
        [AllowAnonymous]
        public IActionResult Login(string username, string pwd)
        {
            if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(pwd))
            {
                var claims = new[] {
                    new Claim(JwtRegisteredClaimNames.Nbf,$"{new DateTimeOffset(DateTime.Now).ToUnixTimeSeconds()}"),//生效时间
                    new Claim(JwtRegisteredClaimNames.Exp,$"{new DateTimeOffset(DateTime.Now.AddMinutes(1)).ToUnixTimeSeconds()}"),//过期时间
                    new Claim(ClaimTypes.Name,username)
                };
                var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configs.GetString("SecureKey")));
                var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                var tokendata = new JwtSecurityToken(
                        issuer: "",//签发人
                        audience: "",//受众
                        claims: claims,
                        expires: DateTime.Now.AddMinutes(1),
                        signingCredentials: creds
                    );
                string token = new JwtSecurityTokenHandler().WriteToken(tokendata);
                return Content(token);
            }
            return Content("登录失败");
        }
    }
}
