package iot.lidee.oauth.service.impl;

import cn.hutool.core.util.IdUtil;
import iot.lidee.oauth.domain.OauthAccessToken;
import iot.lidee.oauth.domain.OauthClientDetails;
import iot.lidee.oauth.domain.OauthCode;
import iot.lidee.oauth.mapper.OauthAccessTokenMapper;
import iot.lidee.oauth.service.IOauthClientDetailsService;
import iot.lidee.oauth.service.IOauthCodeService;
import iot.lidee.oauth.service.OauthAccessTokenService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author fastb
 * @date 2023-09-01 17:20
 */
@Service
public class OauthAccessTokenServiceImpl implements OauthAccessTokenService {

    @Resource
    private OauthAccessTokenMapper oauthAccessTokenMapper;
    @Resource
    private IOauthCodeService oauthCodeService;
    @Resource
    private IOauthClientDetailsService oauthClientDetailsService;

    @Override
    public String selectUserNameByTokenId(String tokenId) {
        return oauthAccessTokenMapper.selectUserNameByTokenId(tokenId);
    }

    @Override
    public OauthAccessToken selectByTokenId(String tokenId) {
        return oauthAccessTokenMapper.selectByTokenId(tokenId);
    }

    @Override
    public void updateOpenIdByTokenId(String tokenId, String openUid) {
        oauthAccessTokenMapper.updateOpenIdByTokenId(tokenId, openUid);
    }

    @Override
    public OauthAccessToken selectByUserName(String userName) {
        return oauthAccessTokenMapper.selectByUserName(userName);
    }

    @Override
    public OauthAccessToken grantAuthorizationCodeForAccessToken(OauthClientDetails client, String code, String redirectUri, String state) {
        OauthCode oauthCode = oauthCodeService.consumeAuthorizationCode(code);
        oauthAccessTokenMapper.deleteByUserId(oauthCode.getUserId());
        OauthAccessToken oauthAccessToken = new OauthAccessToken();
        oauthAccessToken.setTokenId(generateRefreshToken());
        oauthAccessToken.setClientId(client.getClientId());
        oauthAccessToken.setUserId(oauthCode.getUserId());
        oauthAccessToken.setRefreshToken(generateRefreshToken());
        oauthAccessToken.setExpiresTime(LocalDateTime.now().plusSeconds(client.getAccessTokenValidity()));
        oauthAccessTokenMapper.insertOauthAccessToken(oauthAccessToken);
        return oauthAccessToken;
    }

    private static String generateRefreshToken() {
        return IdUtil.fastSimpleUUID();
    }

}
