using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Performance.DtoModels;
using Performance.DtoModels.AppSettings;
using Performance.EntityModels;
using Performance.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;

namespace Performance.Api.Controllers
{
    [Route("api/[controller]")]
    public class AccountController : Controller
    {
        private UserService _userService;
        private RoleService _roleService;
        private IMemoryCache _memoryCache;
        private Application _options;
        private ClaimService _claim;
        private HospitalService _hospitalService;

        public AccountController(UserService userService,
            HospitalService hospitalService,
            RoleService roleService,
            IMemoryCache memoryCache,
            IOptions<Application> options,
            ClaimService claim)
        {
            _userService = userService;
            _roleService = roleService;
            _memoryCache = memoryCache;
            _hospitalService = hospitalService;
            _options = options.Value;
            _claim = claim;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <remarks>
        /// Sample request:
        ///
        ///     POST /Todo
        ///     {
        ///       "logintype": 2,
        ///       "account": "admin",
        ///       "password": "b59c67bf196a4758191e42f76670ceba",
        ///       "appname": "string",
        ///       "device": "web"
        ///     }
        ///
        /// </remarks>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("login")]
        [AllowAnonymous]
        public ApiResponse<JwtToken> Login([FromBody] LoginRequest request)
        {
            var user = _userService.Login(request);
            if (user == null)
                return new ApiResponse<JwtToken>(ResponseType.Fail, "用户不存在");

            var claims = new Claim[]
            {
                new Claim(JwtClaimTypes.Id, user.UserID.ToString()),
                new Claim(JwtClaimTypes.Login, user.Login),
                new Claim(JwtClaimTypes.RealName,  user.RealName),
                new Claim(JwtClaimTypes.Mail, user.Mail??""),
                new Claim(JwtClaimTypes.AppName, request.AppName ?? ""),
                new Claim(JwtClaimTypes.Device, request.Device ?? ""),
                new Claim(JwtClaimTypes.Department, user.Department ?? ""),
                new Claim(JwtClaimTypes.QuickLogin, EQuickLogin.NO.ToString()),
            };

            var jwtToken = JwtTokenHelper.GenerateToken(claims, _options.ExpirationMinutes);
            return new ApiResponse<JwtToken>(ResponseType.OK, jwtToken);
        }

        /// <summary>
        /// 快速登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("quick/login")]
        [HttpPost]
        [AllowAnonymous]
        public ApiResponse<JwtToken> QuickLogin([FromBody] ResetPwdRequest request)
        {
            var user = _userService.QuickLogin(request.TargetUserId, request.CurrentUserId, request.Password);
            if (user == null)
                return new ApiResponse<JwtToken>(ResponseType.Fail, "用户不存在");

            var claims = new Claim[]
            {
                new Claim(JwtClaimTypes.Id, user.UserID.ToString()),
                new Claim(JwtClaimTypes.Login, user.Login),
                new Claim(JwtClaimTypes.RealName,  user.RealName),
                new Claim(JwtClaimTypes.Mail, user.Mail??""),
                new Claim(JwtClaimTypes.AppName, request.AppName ?? ""),
                new Claim(JwtClaimTypes.Device, request.Device ?? ""),
                new Claim(JwtClaimTypes.Department, user.Department ?? ""),
                new Claim(JwtClaimTypes.QuickLogin, EQuickLogin.YES.ToString()),
            };

            var jwtToken = JwtTokenHelper.GenerateToken(claims, _options.ExpirationMinutes);
            return new ApiResponse<JwtToken>(ResponseType.OK, jwtToken);
        }

        /// <summary>
        /// 刷新登录JWT TOKEN
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("refresh")]
        public ApiResponse<JwtToken> Refresh()
        {
            var userClaim = _claim.GetUserClaim();
            var claims = new Claim[]
            {
                new Claim(JwtClaimTypes.Id, userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.Id)?.Value??""),
                new Claim(JwtClaimTypes.Login, userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.Login)?.Value??""),
                new Claim(JwtClaimTypes.RealName,  userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.RealName)?.Value??""),
                new Claim(JwtClaimTypes.Mail, userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.Mail)?.Value??""),
                new Claim(JwtClaimTypes.AppName, userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.AppName)?.Value??""),
                new Claim(JwtClaimTypes.Device, userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.Device)?.Value??""),
                new Claim(JwtClaimTypes.Department, userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.Department)?.Value??""),
            };

            var jwtToken = JwtTokenHelper.GenerateToken(claims, _options.ExpirationMinutes);
            // 设置当前请求Jwt失效
            var jwt = _claim.GetJwtToken();
            //claimService.SetJwtBlacklist(jwt);

            return new ApiResponse<JwtToken>(ResponseType.OK, jwtToken);
        }

        /// <summary>
        /// 查询个人信息
        /// </summary>
        /// <returns></returns>
        [Route("selfinfo")]
        [HttpPost]
        public ApiResponse SelfInfo()
        {
            var userid = _claim.GetUserId();
            var user = _userService.GetUser(userid);

            var userClaim = _claim.GetUserClaim();

            // 如果用户是快捷登录的，则不需要重置密码
            var quickLogin = userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.QuickLogin)?.Value ?? "";
            if (quickLogin.Equals(EQuickLogin.YES.ToString(), StringComparison.OrdinalIgnoreCase))
            {
                user.IsInitialPassword = (int)InitialPassword.修改;
            }

            user.Role = _roleService.GetUserRole(user.UserID);
            user.Hospital = _hospitalService.GetUserHopital(user.UserID);

            int[] roleArray = UnitTypeUtil.Maps.Keys.ToArray();
            user.IsAgainAdmin = user.Role != null ? roleArray.Contains(user.Role.First().Type ?? 0) : false;
            return new ApiResponse(ResponseType.OK, user);
        }

        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("updateself")]
        [HttpPost]
        public ApiResponse<UserResponse> UpdateSelf([CustomizeValidator(RuleSet = "Self"), FromBody] UserRequest request)
        {
            request.ID = _claim.GetUserId();
            var user = _userService.UpdateSelf(request);
            return new ApiResponse<UserResponse>(ResponseType.OK, user);
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        /// <returns></returns>
        [Route("list")]
        [HttpPost]
        public ApiResponse<List<UserResponse>> List([FromBody] UserListRequest request)
        {
            var userList = _userService.GetUserList(_claim.GetUserId(), request.Role);
            return new ApiResponse<List<UserResponse>>(ResponseType.OK, "ok", userList);
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("password")]
        [HttpPost]
        public ApiResponse<UserResponse> Password([FromBody] PasswordRequest request)
        {
            var userid = _claim.GetUserId();
            var user = _userService.UpdatePwd(request, userid);
            return new ApiResponse<UserResponse>(ResponseType.OK, user);
        }

        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        [Route("rolelist")]
        [HttpPost]
        public ApiResponse<List<sys_role>> RoleList()
        {
            var userid = _claim.GetUserId();
            var roleList = _userService.RoleList(userid);
            return new ApiResponse<List<sys_role>>(ResponseType.OK, "ok", roleList);
        }

        /// <summary>
        /// 科室列表
        /// </summary>
        /// <param name="allotId"></param>
        /// <returns></returns>
        [Route("department/{allotId}")]
        [HttpPost]
        public ApiResponse<List<TitleValue>> Department(int allotId)
        {
            var department = _userService.Department(allotId);
            return new ApiResponse<List<TitleValue>>(ResponseType.OK, "ok", department);
        }

        /// <summary>
        /// 演示用户
        /// </summary>
        /// <returns></returns>
        [Route("demo/users")]
        [HttpPost]
        public ApiResponse<List<TitleValue<int>>> DemoUsers()
        {
            var users = _userService.GetDemoUsers();
            return new ApiResponse<List<TitleValue<int>>>(ResponseType.OK, users);
        }

        /// <summary>
        /// 更换角色获取Token
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [Route("demo/token/{userId}")]
        [HttpPost]
        public ApiResponse<JwtToken> DemoUsers(int userId)
        {
            if (userId < 1)
                return new ApiResponse<JwtToken>(ResponseType.ParameterError, "用户不存在,请先创建!");

            var user = _userService.GetDemoUserIdentity(userId);
            var userClaim = _claim.GetUserClaim();
            var claims = new Claim[]
            {
                new Claim(JwtClaimTypes.Id, user.UserID.ToString()),
                new Claim(JwtClaimTypes.Login, user.Login),
                new Claim(JwtClaimTypes.RealName,  user.RealName),
                new Claim(JwtClaimTypes.Mail, user.Mail??""),
                new Claim(JwtClaimTypes.AppName, userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.AppName)?.Value??""),
                new Claim(JwtClaimTypes.Device,userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.Device)?.Value??""),
                new Claim(JwtClaimTypes.Department, user.Department ?? ""),
            };

            var jwtToken = JwtTokenHelper.GenerateToken(claims, _options.ExpirationMinutes);
            return new ApiResponse<JwtToken>(ResponseType.OK, jwtToken);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="userId">重置目标用户id</param>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("reset/{userId}")]
        [HttpPost]
        public ApiResponse<UserResponse> Password(int userId, [FromBody] ResetPwdRequest request)
        {
            var loginUserId = _claim.GetUserId();
            var user = _userService.ResetPwd(userId, loginUserId, request.Password);
            return new ApiResponse<UserResponse>(ResponseType.OK, user);
        }


        #region 多角色

        /// <summary>
        /// 查询用户信息
        /// </summary>
        /// <returns></returns>
        [Route("selfInfos")]
        [HttpPost]
        public ApiResponse SelfInfos([FromBody] UserRequest request)
        {
            var userid = _claim.GetUserId();
            var user = _userService.GetUser(userid);
            user.Role = _roleService.GetUsersRole(user.UserID);
            user.Hospital = _hospitalService.GetUserHopital(user.UserID);

            int[] roleArray = UnitTypeUtil.Maps.Keys.ToArray();
            if (request.Role <= 0)
                user.IsAgainAdmin = user.Role != null ? roleArray.Contains(user.Role.First().Type ?? 0) : false;
            else
                user.IsAgainAdmin = user.Role != null ? roleArray.Contains(user.Role.First(t => t.RoleID == request.Role).Type ?? 0) : false;

            var userClaim = _claim.GetUserClaim();

            // 如果用户是快捷登录的，则不需要重置密码
            var quickLogin = userClaim.FirstOrDefault(t => t.Type == JwtClaimTypes.QuickLogin)?.Value ?? "";
            if (quickLogin.Equals(EQuickLogin.YES.ToString(), StringComparison.OrdinalIgnoreCase))
            {
                user.IsInitialPassword = (int)InitialPassword.修改;
            }

            return new ApiResponse(ResponseType.OK, user);
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("InsertUser")]
        [HttpPost]
        public ApiResponse<UserResponse> InsertUser([CustomizeValidator(RuleSet = "Insert"), FromBody] UserRequest request)
        {
            var userId = _claim.GetUserId();
            var user = _userService.InsertUser(request, userId);
            user.RoleArr = request.RoleArr;
            return new ApiResponse<UserResponse>(ResponseType.OK, user);
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("UpdateUser")]
        [HttpPost]
        public ApiResponse<UserResponse> UpdateUser([CustomizeValidator(RuleSet = "Update"), FromBody] UserRequest request)
        {
            var userId = _claim.GetUserId();

            var user = _userService.UpdateUser(request, userId);
            user.RoleArr = request.RoleArr;
            return new ApiResponse<UserResponse>(ResponseType.OK, user);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("DeleteUser")]
        [HttpPost]
        public ApiResponse DeleteUser([CustomizeValidator(RuleSet = "Delete"), FromBody] UserRequest request)
        {
            return _userService.DeleteUser(request.ID);
        }

        #endregion

        /// <summary>
        /// 批量新增用户表头
        /// </summary>
        /// <returns></returns>
        [Route("GetBatchUserStructrue")]
        [HttpPost]
        public ApiResponse GetBatchUserStructrue()
        {
            var result = _userService.GetUserHandsFlat();
            return new ApiResponse(ResponseType.OK, result);
        }


        /// <summary>
        /// 批量新增用户
        /// </summary>
        /// <returns></returns>
        [Route("BatchSaveUser")]
        [HttpPost]
        public ApiResponse BatchSaveUser([CustomizeValidator(RuleSet = "Insert"), FromBody] UserCollectData data)
        {
            return _userService.SaveUserHandsFlat(data);
        }
    }
}