package com.schisandra.auth.application.handler.oauth;

import com.schisandra.auth.common.enums.OauthType;
import com.schisandra.auth.common.utils.AuthStateRedisCache;
import com.schisandra.auth.infra.entity.OauthConfigInfo;
import com.schisandra.auth.infra.rpc.OauthConfigRpc;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthRequest;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Classname GiteeOauthHandler
 * @BelongsProject: schisandra-cloud-storage
 * @BelongsPackage: com.schisandra.auth.application.handler.oauth
 * @Author: landaiqing
 * @CreateTime: 2024-05-26  13:55
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Slf4j
public class GithubOauthHandler implements SchisandraOauthTypeHandler {

    @Resource
    private OauthConfigRpc oauthConfigRpc;

    @Resource
    private AuthStateRedisCache stateRedisCache;

    @Override
    public OauthType getHandlerType() {
        return OauthType.GitHUB;
    }

    @Override
    public AuthRequest getAuthRequest(String type) {
        OauthConfigInfo gitee = oauthConfigRpc.getOauthConfigInfo(type);
        if (ObjectUtils.isEmpty(gitee)) {
            log.error("github oauth 配置信息获取失败");
            return null;
        }
        return new AuthGithubRequest(AuthConfig.builder()
                .clientId(gitee.getClientId())
                .clientSecret(gitee.getClientSecret())
                .redirectUri(gitee.getRedirectUri())
                .build(),stateRedisCache);
    }
}
