package com.ningdatech.carapi.user.controller;

import java.io.IOException;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ningdatech.basic.model.ApiResponse;
import com.ningdatech.carapi.common.constant.CommonConstant;
import com.ningdatech.carapi.user.security.auth.constant.SessionTimeConstant;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

/**
 * @author LiuXinXin
 * @date 2022/9/29 下午3:26
 */

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

    private final ObjectMapper objectMapper;

    @Value("${login.public-key}")
    private String publicKey;

    //@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 = "/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"),
            @ApiImplicitParam(name = "verificationCode",value = "验证码",required = true,paramType = "form",dataType = "String")})
    public void loginByUsernameAndPasswordAndVerificationCode(@RequestParam("username") String username,
                                           @RequestParam("password") String password,@RequestParam("verificationCode") String verificationCode) {
        // 不实现任何内容，只是为了出api文档
    }

    /**
     * 当需要身份认证时，跳转到这里
     *
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/auth-require")
    @CrossOrigin(originPatterns = "*", allowCredentials = "true", maxAge = 3600)
    public void requireAuthentication(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(401);
        response.getWriter().write(objectMapper.writeValueAsString(
                ApiResponse.of(401, "用户未登录", null)));
    }

    @RequestMapping("/invalid-session")
    @CrossOrigin(originPatterns = "*", allowCredentials = "true", maxAge = 3600)
    public void invalidSession(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        Cookie cookie = new Cookie(CommonConstant.COOKIE_KEY, null);
        String cookiePath = request.getContextPath() + "/";
        cookie.setPath(cookiePath);
        cookie.setMaxAge(SessionTimeConstant.SESSION_TIME_SECONDS);
        response.addCookie(cookie);
        response.setStatus(401);
        response.getWriter().write(objectMapper.writeValueAsString(ApiResponse.of(401, "用户未登录", null)));
    }

    @ApiOperation(value = "获取登录信息加密公钥", notes = "获取登录信息加密公钥")
    @GetMapping("/get-public-key")
    public String getPublicKey(){
        return publicKey;
    }
}
