using Furion.DataEncryption;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Nirvana.Common;
using Nirvana.Common.ApiBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using YBDevice.Application.CommonInfo;
using YBDevice.Entity;

namespace YBDevice.Application.AccountInfo
{
    /// <summary>
    /// 账户管理接口
    /// </summary>
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public class AccountAppService : IDynamicApiController
    {
        private readonly IAccountService _accountService;
        private readonly IHttpContextAccessor _httpContextAccessor;
        public AccountAppService(IAccountService accountService, IHttpContextAccessor httpContextAccessor)
        {
            _accountService = accountService;
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 账户登录
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ResultInfo> LoginAsync(LoginModel model)
        {
            var result= await _accountService.LoginAsync(model);
            if (result.code == ResultState.SUCCESS)
            {
                var data = result.data as LoginInfoDto;
                //设置响应报文头
                _httpContextAccessor.HttpContext.Response.Headers["access-token"] = data.Token;
                _httpContextAccessor.HttpContext.Response.Headers["x-access-token"] = data.RefreshToken;

                var claims = JWTEncryption.ReadJwtToken(data.Token)?.Claims;
                // 创建身份信息
                var claimIdentity = new ClaimsIdentity("AuthenticationTypes.Federation");
                claimIdentity.AddClaims(claims);
                var claimsPrincipal = new ClaimsPrincipal(claimIdentity);

                // 设置 HttpContext.User 并登录
                _httpContextAccessor.HttpContext.User = claimsPrincipal;
                await _httpContextAccessor.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal);
            }
            return result;
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="oldpwd"></param>
        /// <param name="newpwd"></param>
        /// <param name="repwd"></param>
        /// <returns></returns>
        [HttpGet]
        [QueryParameters]
        public async Task<ResultInfo> ChangePwdAsync(string oldpwd, string newpwd, string repwd)
        {
            return await _accountService.ChangePwdAsync(oldpwd, newpwd, repwd);
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="pwd">重置的密码</param>
        /// <returns></returns>
        [HttpGet]
        [QueryParameters]
        public async Task<ResultInfo> RevicePasswordAsync(int id, string pwd)
        {
            return await _accountService.RevicePasswordAsync(id, pwd);
        }
        /// <summary>
        /// 越权登录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [QueryParameters]
        public async Task<ResultInfo> UserEnterAsync(int id)
        {
            return await _accountService.UserEnterAsync(id);
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultInfo> OutLoginAsync()
        {
            await _httpContextAccessor.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            return new ResultInfo(ResultState.SUCCESS, "success");
        }
    }
}
