package com.hotent.auth.server.controller;

import com.hotent.auth.server.service.AuthenticationService;
import com.hotent.auth.server.service.AuthenticationThirdPartyService;
import com.hotent.base.annotation.ApiGroup;
import com.hotent.base.constants.ApiGroupConsts;
import com.hotent.base.exception.CertificateException;
import com.hotent.base.jwt.JwtAuthenticationRequest;
import com.hotent.base.model.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "认证接口")
@ApiGroup(group = {ApiGroupConsts.GROUP_AUTH})
public class AuthenticationThirdPartyController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    AuthenticationService authenticationService;

    @Resource
    AuthenticationThirdPartyService authenticationThirdPartyService;

    @RequestMapping(value = "/sso/loginCode", method = RequestMethod.POST)
    @ApiOperation(value = "登录系统-获取code", httpMethod = "POST", notes = "登录系统-获取code")
    public CommonResult<?> createAuthenticationTokenCode(@RequestBody JwtAuthenticationRequest authenticationRequest) throws AuthenticationException, CertificateException {
        logger.info("调用登录，clientId:{} userName:{}",authenticationRequest.getClientId(),authenticationRequest.getUsername());
        return authenticationThirdPartyService.createAuthenticationTokenCode(authenticationRequest);
    }

    @RequestMapping(value = "/sso/exchangeCode", method = RequestMethod.POST)
    @ApiOperation(value = "登录系统-根据code获取token信息", httpMethod = "POST", notes = "登录系统-根据code获取token信息")
    public ResponseEntity<?> exchangeAuthenticationTokenCode(@RequestParam(value = "code", required = true) String code) throws AuthenticationException, CertificateException {
        return authenticationService.exchangeAuthenticationTokenCode(code);
    }

    @RequestMapping(value = "/sso/develop", method = RequestMethod.POST)
    @ApiOperation(value = "登录系统-根据code获取token信息", httpMethod = "POST", notes = "登录系统-根据code获取token信息")
    public ResponseEntity<?> createAuthenticationTokenDevelop(@RequestBody JwtAuthenticationRequest authenticationRequest) throws AuthenticationException, CertificateException {
        return authenticationThirdPartyService.createAuthenticationTokenDevelop(authenticationRequest);
    }

    @RequestMapping(value = "/app/code/generate", method = RequestMethod.POST, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "获取shortCode", httpMethod = "POST", notes = "获取shortCode")
    public CommonResult<?> generateToken(HttpServletRequest request) throws AuthenticationException, CertificateException {
        return authenticationThirdPartyService.generateShortAuthenticationToken(request);
    }

    @RequestMapping(value = "/app/code/convert", method = RequestMethod.GET, produces = {"application/json; charset=utf-8"})
    @ApiOperation(value = "通过shortCode获取token", httpMethod = "GET", notes = "通过shortCode获取token")
    public CommonResult<?> convertToken(@RequestParam(value = "shortCode", required = true) String shortCode) throws AuthenticationException, CertificateException {
        return authenticationThirdPartyService.convertShortAuthenticationToken(shortCode);
    }

}