package org.springblade.system.bladeuserex.social;

import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.*;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.system.bladeuserex.social.ctwing.CtwingAuthConstants;

public class SocialUtil {

	private static AuthStateCache authStateCache;
	private static SocialConfig socialConfig;

	static {
		socialConfig = SpringUtil.getBean(SocialConfig.class);
	}

	public SocialUtil() {
	}

	public static AuthStateCache getAuthStateCache() {
		if (authStateCache == null) {
			authStateCache = (AuthStateCache) SpringUtil.getBean(AuthStateCache.class);
		}

		return authStateCache;
	}

	public static AuthRequest getAuthRequest(String source) {
		AuthRequest authRequest = null;
		switch (source.toUpperCase()) {
			case "CTWING":
				authRequest = new AuthCtwingRequest(AuthConfig.builder()
					.clientId(CtwingAuthConstants.APP_ID)
					.clientSecret(CtwingAuthConstants.APP_SECRET)
					.redirectUri(socialConfig.getRedirectUri())
					.build());
				break;
			default:
				break;
		}
		if (null == authRequest) {
			throw new AuthException("未获取到有效的Auth配置");
		}
		return authRequest;
	}
}
