package com.css.txw.sso.controller.oauth2;


import cn.hutool.core.bean.BeanUtil;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.txw.sso.api.oauth2.OAuth2TokenApi;
import com.css.txw.sso.api.oauth2.dto.AccessTokenSessionInfo;
import com.css.txw.sso.api.oauth2.dto.OAuth2AccessTokenCheckRespDTO;
import com.css.txw.sso.api.oauth2.dto.OAuth2AccessTokenCreateReqDTO;
import com.css.txw.sso.api.oauth2.dto.OAuth2AccessTokenRespDTO;
import com.css.txw.sso.constants.SsoApiConstants;
import com.css.txw.sso.constants.SsoConstants;
import com.css.txw.sso.pojo.domain.oauth2.OAuth2AccessTokenDO;
import com.css.txw.sso.pojo.dto.session.*;
import com.css.txw.sso.service.oauth2.OAuth2TokenService;
import com.css.ggzc.framework.common.pojo.CommonResult;
import com.css.ggzc.framework.common.util.object.BeanUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.css.ggzc.framework.common.pojo.CommonResult.success;

@RequestMapping("/oauth2/token")
@RestController
@Validated
@Slf4j
public class OAuth2TokenController implements OAuth2TokenApi {

    @Resource
    private OAuth2TokenService oauth2TokenService;

    @Override
    @PostMapping("/create")
    @Operation(description = "创建访问令牌")
    public CommonResult<OAuth2AccessTokenRespDTO> createAccessToken(@Valid @RequestBody OAuth2AccessTokenCreateReqDTO reqDTO) {
        OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.createAccessToken(
                reqDTO.getUserId(), reqDTO.getClientId(), reqDTO.getScopes());
        final OAuth2AccessTokenRespDTO bean = BeanUtils.toBean(accessTokenDO, OAuth2AccessTokenRespDTO.class);
        bean.setUserId(accessTokenDO.getYhUuid());
        bean.setExpiresTime(accessTokenDO.getGqsj());
        return success(bean);
    }

    @GetMapping("/check")
    @Override
    public CommonResult<OAuth2AccessTokenCheckRespDTO> checkAccessToken(@RequestParam("accessToken") String accessToken) {
        final SessionInfo sessionInfo = oauth2TokenService.checkAccessToken(accessToken);
        log.info("/check getSessionfromRedis:{}",sessionInfo);
        return success(covertSessionInfo(accessToken,sessionInfo));
    }

    @DeleteMapping("/remove")
    @Override
    public CommonResult<OAuth2AccessTokenRespDTO> removeAccessToken(@RequestParam("accessToken") String accessToken) {
        OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.removeAccessToken(SsoConstants.CLIENT_DEFAULT,accessToken);
        final OAuth2AccessTokenRespDTO bean = BeanUtils.toBean(accessTokenDO, OAuth2AccessTokenRespDTO.class);
        bean.setUserId(accessTokenDO.getYhUuid());
        bean.setExpiresTime(accessTokenDO.getGqsj());
        return success(bean);
    }

    @PutMapping("/refresh")
    @Override
    public CommonResult<OAuth2AccessTokenRespDTO> refreshAccessToken(@RequestParam("refreshToken") String refreshToken,
                                                                     @RequestParam("clientId") String clientId) {
        OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.refreshAccessToken(refreshToken, clientId);
        final OAuth2AccessTokenRespDTO bean = BeanUtils.toBean(accessTokenDO, OAuth2AccessTokenRespDTO.class);
        bean.setUserId(accessTokenDO.getYhUuid());
        bean.setExpiresTime(accessTokenDO.getGqsj());
        return success(BeanUtils.toBean(accessTokenDO, OAuth2AccessTokenRespDTO.class));
    }

    private OAuth2AccessTokenCheckRespDTO covertSessionInfo(String accessToken,SessionInfo sessionInfo){
        final OAuth2AccessTokenCheckRespDTO respDTO = new OAuth2AccessTokenCheckRespDTO();

        final AccessTokenInfo tokenInfo = sessionInfo.getTokenInfo();
        if (!accessToken.equals(tokenInfo.getAccessToken())){
            respDTO.setHasRefreshedToken(true);
            respDTO.setNewAccessToken(tokenInfo.getAccessToken());
            respDTO.setNewExpireTime(tokenInfo.getGqsj());
        }else {
            respDTO.setHasRefreshedToken(false);
        }

        final AccessTokenSessionInfo accessTokenSessionInfo = new AccessTokenSessionInfo();
        YhInfo yhxx = sessionInfo.getYhxx();
        BeanUtil.copyProperties(yhxx,accessTokenSessionInfo,false);
        accessTokenSessionInfo.setYhUuid(yhxx.getYhuuid());
        String yhlx = sessionInfo.getCurrentYhlx();
        if (!GyUtils.isNull(yhlx)){
            accessTokenSessionInfo.setYhlx(yhlx);
        }
        accessTokenSessionInfo.setQyuuid(sessionInfo.getQyuuid());
        accessTokenSessionInfo.setQymc(sessionInfo.getQymc());
        accessTokenSessionInfo.setNsrsbh(sessionInfo.getNsrsbh());
        log.info("/check接口返回sessionInfo:,{}",accessTokenSessionInfo);
        respDTO.setSessionInfo(accessTokenSessionInfo);
        return respDTO;
    }


}
