package com.ningdatech.pmapi.user.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.ningdatech.basic.util.StrPool;
import com.ningdatech.pmapi.common.constant.BizConst;
import com.ningdatech.pmapi.user.security.auth.constants.SessionTimeConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 用户鉴权表 前端控制器
 * </p>
 *
 * @author Liuxinxin
 * @since 2023-01-04
 */

@RestController
@RequestMapping("/api/v1/user/auth")
@Api(tags = {"用户鉴权-相关接口"})
@RequiredArgsConstructor
public class UserAuthController {

    private final ObjectMapper objectMapper;

    @PostMapping(value = "/login", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiOperation(value = "登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "identifier", value = "账号", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "credential", value = "凭证", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "loginType", value = "DING_QR_LOGIN 浙政钉扫码登陆,PHONE_VERIFICATION_CODE_LOGIN 手机号验证码登陆"
                    , required = true, paramType = "form", dataType = "String")})
    public void credentialLogin(@RequestParam(value = "identifier", required = false) String identifier,
                                @RequestParam(value = "credential", required = false) String credential,
                                @RequestParam("loginType") String loginType) {
        // 不实现任何内容，只是为了出api文档
    }

    @PostMapping(value = "/logout")
    @ApiOperation(value = "退出登陆")
    public void logout() {
        // 不实现任何内容，具体实现交由Spring Security进行管理
    }

    /**
     * 当需要身份认证时，跳转到这里
     */
    @GetMapping("/auth-require")
    @CrossOrigin(originPatterns = "*", allowCredentials = "true", maxAge = 3600)
    public void requireAuthentication(HttpServletResponse response) throws IOException {
        response.setContentType(StrPool.CONTENT_TYPE);
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        response.getWriter().write(objectMapper.writeValueAsString(BizConst.UNAUTHENTICATED));
    }

    /**
     * 设置session失效
     */
    @GetMapping("/invalid-session")
    @CrossOrigin(originPatterns = "*", allowCredentials = "true", maxAge = 3600)
    public void invalidSession(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType(StrPool.CONTENT_TYPE);
        Cookie cookie = new Cookie(BizConst.COOKIE_KEY, null);
        cookie.setPath(request.getContextPath() + "/");
        cookie.setMaxAge(SessionTimeConstant.SESSION_TIME_SECONDS);
        response.addCookie(cookie);
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        response.getWriter().write(objectMapper.writeValueAsString(BizConst.UNAUTHENTICATED));
    }

}
