package com.ytpm.controller;

import com.ytpm.auth.AuthService;
import com.ytpm.auth.CaptchaService;
import com.ytpm.general.RepMessage;
import com.ytpm.general.Result;
import com.ytpm.general.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.Principal;
import java.util.Locale;
import java.util.Map;

@Api(tags = "认证鉴权管理器")
@Slf4j(topic = "oauth-controller")
@RestController
@RequestMapping("/oauth")
public class OauthController {

    @Resource
    private AuthService authService;
    @Resource
    private CaptchaService captchaService;

    /**
     * 获取图片验证码
     *
     */
    @ApiImplicitParam(name = "uuid", value = "根据当前时间生成唯一UUID用于本次登录验证码", required = true, paramType = "path")
    @GetMapping(value = "/captcha.jpg")
    public void getCaptcha(HttpServletResponse response, String uuid) throws IOException {
        captchaService.create(response, uuid);
    }

    /**
     * 重写/oauth/token接口
     *
     */
    @ApiOperation(value = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名",required = true, paramType = "x-www-form-urlencoded"),
            @ApiImplicitParam(name = "password", value = "密码",required = true, paramType = "x-www-form-urlencoded"),
            @ApiImplicitParam(name = "scope", value = "作用范围",required = false, paramType = "x-www-form-urlencoded"),
            @ApiImplicitParam(name = "grant_type", value = "授权类型",required = true, paramType = "x-www-form-urlencoded"),
            @ApiImplicitParam(name = "uuid", value = "uuid 客户端获取验证码时生成的唯一ID",required = true, paramType = "x-www-form-urlencoded"),
            @ApiImplicitParam(name = "captcha", value = "验证码",required = true, paramType = "x-www-form-urlencoded"),
    })
    @PostMapping("/token")
    public Result postAccessToken(Principal principal,
                                  @RequestParam Map<String, String> parameters) {
        return authService.createAccessToken(principal, parameters);
    }

    /**
     * 认证注销
     *
     * @return authLogout
     */
    @ApiOperation(value = "注销登录")
    @PostMapping(value = "/logout")
    public Result logOut(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (null == token) {
            log.error("认证注销，未获取到登录信息");
            return Result.resultFail(StatusCode.MANAGE_NOT_LOGIN, RepMessage.NOT_LOGIN);
        } else {
            token = token.toLowerCase(Locale.ROOT).startsWith("bearer")?token.substring(6).trim():token;
        }
        return authService.logout(token);
    }
}
