package org.springblade.auth.utils;

import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.*;
import org.springblade.auth.model.ThirdPartyLogin;


public class ThirdPartyAuthUtil {

    public static AuthRequest getAuthRequest(ThirdPartyLogin login){
        AuthRequest authRequest = null;
        System.out.println(login.getLoginType().getValue().toLowerCase());
        switch (login.getLoginType().getValue().toLowerCase()) {
            case "dingtalk":
                authRequest = new AuthDingTalkRequest(AuthConfig.builder()
                        .clientId(login.getClientId())
                        .clientSecret(login.getClientSecret())
                        .redirectUri(login.getRedirectUri())
                        .build());
                break;
            case "alipay":
                // 支付宝在创建回调地址时，不允许使用localhost或者127.0.0.1，所以这儿的回调地址使用的局域网内的ip
                authRequest = new AuthAlipayRequest(AuthConfig.builder()
                        .clientId(login.getClientId())
                        .clientSecret(login.getClientSecret())
                        .alipayPublicKey(login.getAlipayPublicKey())
                        .redirectUri(login.getRedirectUri())
                        .build());
                break;
            case "qq":
                authRequest = new AuthQqRequest(AuthConfig.builder()
                        .clientId(login.getClientId())
                        .clientSecret(login.getClientSecret())
                        .redirectUri(login.getRedirectUri())
                        .build());
                break;
            case "wechat":
                authRequest = new AuthWeChatOpenRequest(AuthConfig.builder()
                        .clientId(login.getClientId())
                        .clientSecret(login.getClientSecret())
                        .redirectUri(login.getRedirectUri())
                        .build());
                break;
            case "douyin":
                authRequest = new AuthDouyinRequest(AuthConfig.builder()
                        .clientId(login.getClientId())
                        .clientSecret(login.getClientSecret())
                        .redirectUri(login.getRedirectUri())
                        .build());
                break;
            case "wechat_enterprise":
                authRequest = new AuthWeChatEnterpriseQrcodeRequest(AuthConfig.builder()
                        .clientId(login.getClientId())
                        .clientSecret(login.getClientSecret())
                        .redirectUri(login.getRedirectUri())
                        .agentId(login.getAgentId())
                        .build());
                break;
            case "feishu":
                authRequest = new AuthFeishuRequest(AuthConfig.builder()
                        .clientId(login.getClientId())
                        .clientSecret(login.getClientSecret())
                        .redirectUri(login.getRedirectUri())
                        .build());
                break;
            default:
                break;
        }
        if (null == authRequest) {
            throw new AuthException("未获取到有效的Auth配置");
        }
        return authRequest;
    }
}
