package yem.base.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import kd.bos.cache.CacheConfigInfo;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.LocalMemoryCache;
import kd.bos.session.EncreptSessionUtils;

import java.util.Base64;

/**
 * @auther: longh
 * @Date: 2023/12/29 20:32
 * @className TokenUtil
 * @description 插件
 */
public class TokenUtil {
    private String mainUrl;
    private String appId;
    private String appSecuret;
    private String tenantid;
    private String accountId;
    private String user;
    private String usertype = "Mobile";
    private String language = "zh_CN";

    public TokenUtil(String mainUrl, String appId, String appSecuret, String user) {
        this.mainUrl = mainUrl;
        this.appId = appId;
        this.appSecuret = appSecuret;
        this.user = user;
    }

    public TokenUtil(String mainUrl, String appId, String appSecuret, String tenantid, String accountId, String user) {
        this.mainUrl = mainUrl;
        this.appId = appId;
        this.appSecuret = appSecuret;
        this.tenantid = tenantid;
        this.accountId = accountId;
        this.user = user;
    }

    public TokenUtil(String mainUrl, String appId, String appSecuret, String tenantid, String accountId, String user, String usertype) {
        this.mainUrl = mainUrl;
        this.appId = appId;
        this.appSecuret = appSecuret;
        this.tenantid = tenantid;
        this.accountId = accountId;
        this.user = user;
        this.usertype = usertype;
    }

    public static String getEncreptURL(String url) {
        return EncreptSessionUtils.encryptSession(url);
    }

    public String getAccessToken() {
        String access_token = null;
        try {
            CacheConfigInfo localConfig = new CacheConfigInfo();
            localConfig.setMaxItemSize(100);
            localConfig.setTimeout(300);
            String tokenKey = String.format("Token_Cache_%s_%s_%s_%s_%s_%s", mainUrl, appId, tenantid, accountId, user, usertype);
            tokenKey = Base64.getEncoder().encodeToString(tokenKey.getBytes());
            LocalMemoryCache localcache = CacheFactory.getCommonCacheFactory().$getOrCreateLocalMemoryCache("yem", tokenKey, localConfig);
            access_token = (String) localcache.get("access_token");
            if (YEM.isEmpty(access_token)) {
                JSONObject param = new JSONObject();
                param.put("appId", appId);
                param.put("appSecuret", appSecuret);
                param.put("tenantid", tenantid);
                param.put("accountId", accountId);
                param.put("language", language);
                String body = HttpHelper.httpPost(mainUrl + "/api/getAppToken.do", param.toJSONString());
                String apptoken = JSON.parseObject(body).getJSONObject("data").getString("app_token");

                param = new JSONObject();
                param.put("user", user);
                param.put("apptoken", apptoken);
                param.put("tenantid", tenantid);
                param.put("accountId", accountId);
                param.put("usertype", usertype);
                param.put("language", language);
                body = HttpHelper.httpPost(mainUrl + "/api/login.do", param.toJSONString());
                JSONObject data = JSON.parseObject(body).getJSONObject("data");
                if (data.getBoolean("success")) {
                    access_token = data.getString("access_token");
                    localcache.put("access_token", access_token);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return access_token;
    }

}
