using System.Security.Claims;
using System.Threading.Tasks;
using GxPress.Api.Tools;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Entity;
using GxPress.EnumConst;
using GxPress.Repository.Interface;
using GxPress.Request.Admin;
using GxPress.Result.Admin;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace GxPress.Api.AdminControllers
{
    /// <summary>
    /// 管理员
    /// </summary>
    [Route("api/admin/admin")]
    [ApiController]
    [Authorize(Roles = Roles.Admin)]
    public class AdminController : ControllerBase
    {
        private readonly JwtOptions _jwtOptions;
        private readonly ILogger<AdminController> _logger;
        private readonly IAdminRepository _adminRepository;

        public AdminController(IOptions<JwtOptions> jwtOptions, ILogger<AdminController> logger, IAdminRepository adminRepository)
        {
            _jwtOptions = jwtOptions.Value;
            _logger = logger;
            _adminRepository = adminRepository;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("signin")]
        [AllowAnonymous]
        public async Task<AdminSignInResult> SignIn(AdminSignInRequest request)
        {
            var result = await _adminRepository.SignInAsync(request);
            var claims = new[]
            {
                new Claim(ClaimTypes.NameIdentifier,result.AdminId.ToString()),
                new Claim(ClaimTypes.Role,AccountTypeConst.Admin.ToString())
            };
            result.Token = TokenHelper.BuildToken(_jwtOptions, claims);
            _logger.LogInformation("admin token:{token}", result.Token);
            return result;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("signup")]
        public async Task<int> SignUp([FromBody]AdminSignUpRequest request)
        {
            return await _adminRepository.SignUpAsync(request);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> Delete(int id)
        {
            return await _adminRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<AdminDetailResult> Get(int id)
        {
            return await _adminRepository.GetDetailAsync(id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<bool> Update(int id, [FromBody] Admin request)
        {
            return await _adminRepository.UpdateAsync(id, request);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("updatemenu")]
        public async Task<bool> UpdateMenu( [FromBody] Admin request)
        {
            return await _adminRepository.UpdateQAsync(request);
        }
        /// <summary>
        /// 分页列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("page")]
        public async Task<PagedList<AdminPageResult>> GetPagedList([FromBody]AdminPageSearchRequest request)
        {
            return await _adminRepository.GetPagedList(request);
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("updatepassword")]
        public async Task<bool> UpdatePassWord([FromBody] AdminUpdatePassWordRequest request)
        {
            return await _adminRepository.UpdatePassWord(request);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("admin-update-password")]
        public async Task<bool> AdminUpdatePassWord([FromBody] AdminUpdatePassWordRequest request)
        {
            return await _adminRepository.AdminUpdatePassWord(request);
        }
    }
}