package com.cm.common.dingding.manager.app;

import com.cm.common.dingding.config.properties.DingDingAppProperties;
import com.cm.common.exception.DingDingAccessTokenException;
import com.cm.common.exception.WechatAccessTokenException;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.taobao.api.ApiException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.client.RestTemplate;

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

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DingDingAppManager
 * @Description: 钉钉APP管理
 * @Author: WangGeng
 * @Date: 2020/8/31 17:56
 * @Version: 1.0
 **/
public class DingDingAppManager {
    private static final Logger LOG = LoggerFactory.getLogger(DingDingAppManager.class);
    private static final DingDingAppManager dingDingAppManager = DingDingAppManagerBuilder.dingDingAppManager;
    private DingDingAppProperties dingDingAppProperties;
    private String accessToken;
    private long updateTime = 0L;

    private DingDingAppManager() {
    }

    public static DingDingAppManager getInstance() {
        return dingDingAppManager;
    }

    /**
     * 刷新AccessToken，超过1小时30分刷新一次
     */
    public void refreshAccessToken() {
        long currentTime = System.currentTimeMillis();
        if ((currentTime - this.updateTime) >= 5400000L) {
            LOG.debug("刷新钉钉 App AccessToken 开始");
            this.accessToken = getNewAccessToken();
            LOG.debug("accessToken: {}", this.accessToken);
            this.updateTime = System.currentTimeMillis();
            LOG.debug("刷新钉钉 App AccessToken 结束");
        }
    }

    /**
     * 获取access_token
     *
     * @return
     */
    private String getNewAccessToken() {
        DefaultDingTalkClient dingTalkClient = new DefaultDingTalkClient(dingDingAppProperties.getTokenUrl());
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(dingDingAppProperties.getAppKey());
        request.setAppsecret(dingDingAppProperties.getAppSecret());
        request.setHttpMethod("GET");
        try {
            OapiGettokenResponse response = dingTalkClient.execute(request);
            if (response.getErrcode() != 0) {
                throw new DingDingAccessTokenException("获取钉钉AccessToken异常");
            }
            return response.getAccessToken();
        } catch (ApiException e) {
            LOG.error(e.getMessage(), e);
            throw new DingDingAccessTokenException("获取钉钉AccessToken异常");
        }
    }

    public String getAccessToken() {
        return accessToken == null ? "" : accessToken.trim();
    }

    public void setDingDingAppProperties(DingDingAppProperties dingDingAppProperties) {
        this.dingDingAppProperties = dingDingAppProperties;
    }

    private static class DingDingAppManagerBuilder {
        public static final DingDingAppManager dingDingAppManager = new DingDingAppManager();
    }

}
