package com.simuwang.manage.api;

import cn.hutool.core.map.MapUtil;
import com.simuwang.base.config.DaqProperties;
import com.simuwang.logging.SystemLog;
import com.simuwang.manage.dto.LoginUser;
import com.simuwang.manage.dto.UserInfoVO;
import com.simuwang.manage.service.LoginService;
import com.simuwang.shiro.core.ShiroToken;
import com.simuwang.shiro.core.ShiroUser;
import com.simuwang.shiro.core.jwt.JwtContext;
import com.simuwang.shiro.utils.UserUtils;
import com.smppw.common.pojo.ResultVo;
import com.smppw.common.pojo.enums.status.ResultCode;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 登录相关接口
 */
@SystemLog(value = "登录相关")
@RestController
@RequestMapping("/v1")
public class LoginController {
    private final JwtContext jwtContext;
    private final DaqProperties properties;
    private final LoginService loginService;

    public LoginController(JwtContext jwtContext, DaqProperties properties, LoginService loginService) {
        this.jwtContext = jwtContext;
        this.properties = properties;
        this.loginService = loginService;
    }

    /**
     * 获取rsk公钥
     *
     * @return /
     */
    @SystemLog(value = "获取公钥", type = SystemLog.Type.QUERY)
    @GetMapping("rsa-key")
    public Map<String, Object> getRsaKey() {
        return MapUtil.<String, Object>builder("rsaKey", this.properties.getSecurityRsa().getPublicKey()).build();
    }

    /**
     * 用户登录
     *
     * @param loginUser 登录用户账号和密码
     *                  //     * @param response  响应体对象
     * @return /
     */
    @SystemLog(value = "登录", type = SystemLog.Type.LOGIN)
    @PostMapping("login")
    public ResultVo<String> login(@RequestBody LoginUser loginUser) {
        ShiroToken shiroToken = new ShiroToken(loginUser.getUsername(), loginUser.getPassword());
        Subject subject = UserUtils.getSubject();
        subject.login(shiroToken);

        String token = jwtContext.generateToken(loginUser.getUsername());
        this.jwtContext.setUserCache(token);
        // 加这response会导致响应头和shiro的默认字符集存在冲突，导致接口500
//        response.setHeader(JwtContext.HEADER, token);
//        response.setHeader("Access-control-Expost-Headers", JwtContext.HEADER);
        return ResultVo.ok(ResultCode.SUCCESS.getCode(), "登录成功", token);
    }

    /**
     * 退出登录接口，登录用户才能访问
     */
    @SystemLog(value = "登出", type = SystemLog.Type.LOGOUT)
    @RequiresAuthentication
    @PostMapping("/logout")
    public ResultVo<Boolean> logout() {
        Subject subject = UserUtils.getSubject();
        ShiroUser shiroUser = UserUtils.getLoginUser(subject);
        this.jwtContext.cleanUserCache(shiroUser.getUsername());
        subject.logout();
        return ResultVo.ok(ResultCode.SUCCESS.getCode(), "退出成功", true);
    }

    /**
     * 获取当前用户的角色权限信息（只在登录时获取一次）
     *
     * @return 当前登录用户的角色权限信息
     */
    @RequiresAuthentication
    @GetMapping("/user-info")
    public UserInfoVO getUserInfo() {
        return this.loginService.getUserInfo();
    }
}
