package com.ningdatech.kqapi.user.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.ningdatech.basic.model.ApiResponse;
import com.ningdatech.kqapi.common.constant.BizConst;
import com.ningdatech.kqapi.common.util.StrPool;
import com.ningdatech.kqapi.security.constant.AuthConst;
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;

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

    private final ObjectMapper objectMapper;

    public static final ApiResponse<Void> LOGOUT_RET = ApiResponse.of(HttpStatus.UNAUTHORIZED.value(), "已退出登录");


    @PostMapping(value = "/login/password", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiOperation(value = "账号密码的登陆方式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "form", dataType = "String")
    })
    public void loginByUsernameAndPassword(@RequestParam("username") String username,
                                           @RequestParam("password") String password) {
        // 不实现任何内容，只是为了出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(AuthConst.SESSION_EXPIRED);
        response.addCookie(cookie);
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        response.getWriter().write(objectMapper.writeValueAsString(LOGOUT_RET));
    }

}
