package com.cm.common.plugin.oauth.token;

import com.alibaba.fastjson.JSONObject;
import com.cm.common.config.properties.ApiPathProperties;
import com.cm.common.config.properties.OauthClientProperties;
import com.cm.common.config.properties.OauthProperties;
import com.cm.common.plugin.pojo.bos.ClientTokenBO;
import com.cm.common.plugin.utils.RestTemplateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: ClientTokenManager
 * @Description: 客户单token
 * @Author: WangGeng
 * @Date: 2019/10/16 12:22 上午
 * @Version: 1.0
 **/
public class ClientTokenManager {

    private static final Logger LOG = LoggerFactory.getLogger(ClientTokenManager.class);
    private static ClientTokenManager clientTokenManager = ClientTokenBuilder.clientTokenManager;
    private ClientTokenBO clientToken;
    private RestTemplateUtil restTemplateUtil;
    private OauthProperties oauthProperties;
    private OauthClientProperties oauthClientProperties;
    private ApiPathProperties apiPathProperties;

    private ClientTokenManager() {
    }

    public static ClientTokenManager getInstance() {
        return clientTokenManager;
    }

    public static void setClientTokenManager(ClientTokenManager clientTokenManager) {
        ClientTokenManager.clientTokenManager = clientTokenManager;
    }

    public ClientTokenBO getClientToken() {
        if (clientToken == null || (System.currentTimeMillis() - clientToken.getCreateTime()) / 1000 >= clientToken.getExpiresIn()) {
            LOG.debug("客户端Token失效，获取token");
            Map<String, Object> params = new HashMap<>(4);
            params.put("grant_type", "client_credentials");
            params.put("client_id", oauthClientProperties.getClientId());
            params.put("client_secret", oauthClientProperties.getClientSecret());
            params.put("scope", "all");
            String result = restTemplateUtil.doPostFormNormal(String.format("%s/oauth_client/token", apiPathProperties.getUserCenter()), params);
            if (StringUtils.isBlank(result)) {
                LOG.error("客户端获取token失效");
            } else {
                JSONObject accessTokenObject = JSONObject.parseObject(result);
                clientToken = new ClientTokenBO();
                clientToken.setAccessToken(accessTokenObject.getString("access_token"));
                clientToken.setTokenType(accessTokenObject.getString("token_type"));
                clientToken.setExpiresIn(accessTokenObject.getInteger("expires_in"));
                clientToken.setScope(accessTokenObject.getString("scope"));
                clientToken.setJti(accessTokenObject.getString("jti"));
                clientToken.setCreateTime(System.currentTimeMillis());
            }
        }
        return clientToken;
    }

    public RestTemplateUtil getRestTemplateUtil() {
        return restTemplateUtil;
    }

    public void setRestTemplateUtil(RestTemplateUtil restTemplateUtil) {
        this.restTemplateUtil = restTemplateUtil;
    }

    public OauthProperties getOauthProperties() {
        return oauthProperties;
    }

    public void setOauthProperties(OauthProperties oauthProperties) {
        this.oauthProperties = oauthProperties;
    }

    public OauthClientProperties getOauthClientProperties() {
        return oauthClientProperties;
    }

    public void setOauthClientProperties(OauthClientProperties oauthClientProperties) {
        this.oauthClientProperties = oauthClientProperties;
    }

    private static class ClientTokenBuilder {
        public static ClientTokenManager clientTokenManager = new ClientTokenManager();
    }

    public void setApiPathProperties(ApiPathProperties apiPathProperties) {
        this.apiPathProperties = apiPathProperties;
    }
}
