package com.gtiit.shr.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.gtiit.shr.config.TimerExpireHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

@Component
public class OATokenUtil {
    @Autowired
    FinalParamUtil finalParamUtil;
    private static final TimerExpireHashMap<String, String> SYSTEM_CACHE = new TimerExpireHashMap();


    public Map<String, Object> regist(String address) {
        RSA rsa = new RSA();
        String publicKey = rsa.getPublicKeyBase64();
        String privateKey = rsa.getPrivateKeyBase64();
        SYSTEM_CACHE.put("LOCAL_PRIVATE_KEY", privateKey);
        SYSTEM_CACHE.put("LOCAL_PUBLIC_KEY", publicKey);
        String data = ((HttpRequest)((HttpRequest)HttpRequest.post(address + "/api/ec/dev/auth/regist").header("appid", this.finalParamUtil.getAPPID())).header("cpk", publicKey)).timeout(2000).execute().body();
        System.out.println("testRegist()：" + data);
        Map<String, Object> datas = JSONUtil.parseObj(data);
        SYSTEM_CACHE.put("SERVER_PUBLIC_KEY", StrUtil.nullToEmpty((String)datas.get("spk")));
        SYSTEM_CACHE.put("SERVER_SECRET", StrUtil.nullToEmpty((String)datas.get("secrit")));
        return datas;
    }

    public Map<String, Object> getoken(String address) {
        String secret = (String)SYSTEM_CACHE.get("SERVER_SECRET");
        String spk = (String)SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");
        if (Objects.isNull(secret) || Objects.isNull(spk)) {
            this.regist(address);
            secret = (String)SYSTEM_CACHE.get("SERVER_SECRET");
            spk = (String)SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");
        }

        RSA rsa = new RSA((String)null, spk);
        String encryptSecret = rsa.encryptBase64(secret, CharsetUtil.CHARSET_UTF_8, KeyType.PublicKey);
        String data = ((HttpRequest)((HttpRequest)((HttpRequest)HttpRequest.post(address + "/api/ec/dev/auth/applytoken").header("appid", this.finalParamUtil.getAPPID())).header("secret", encryptSecret)).header("time", "3600")).execute().body();
        System.out.println("getoken()：" + data);
        Map<String, Object> datas = JSONUtil.parseObj(data);
        SYSTEM_CACHE.put("SERVER_TOKEN", StrUtil.nullToEmpty((String)datas.get("token")));
        Long expireSecond = 1800L;
        SYSTEM_CACHE.setKeyExpireTime("SERVER_TOKEN", expireSecond);
        return datas;
    }

    public String restful(String api, String jsonParams, String menthType) {
        String token = (String)SYSTEM_CACHE.get("SERVER_TOKEN");
        if (StrUtil.isEmpty(token)) {
            token = (String)this.getoken(this.finalParamUtil.getOaUrl()).get("token");
        }

        String spk = (String)SYSTEM_CACHE.get("SERVER_PUBLIC_KEY");
        RSA rsa = new RSA((String)null, spk);
        String encryptUserid = rsa.encryptBase64("1", CharsetUtil.CHARSET_UTF_8, KeyType.PublicKey);
        if ("sendAPIByAddAdmin".equals(menthType)) {
            return this.sendAPIByAddAdmin(this.finalParamUtil.getOaUrl() + api, token, encryptUserid, jsonParams);
        } else if ("sendAPIByAddPosition".equals(menthType)) {
            return this.sendAPIByAddPosition(this.finalParamUtil.getOaUrl() + api, token, encryptUserid, jsonParams);
        } else {
            return "sendAPIByAddPerson".equals(menthType) ? this.sendAPIByAddPerson(this.finalParamUtil.getOaUrl() + api, token, encryptUserid, jsonParams) : "未找到相关调用代码";
        }
    }

    public String sendAPIByAddAdmin(String api, String token, String encryptUserid, String jsonParams) {
        String data = ((HttpRequest)((HttpRequest)((HttpRequest)HttpRequest.get(api).header("appid", this.finalParamUtil.getAPPID())).header("token", token)).header("userid", encryptUserid)).body(jsonParams).execute().body();
        System.out.println("testRestful()：" + data);
        return data;
    }

    public String sendAPIByAddPosition(String api, String token, String encryptUserid, String jsonParams) {
        String data = ((HttpRequest)((HttpRequest)((HttpRequest)HttpRequest.get(api).header("appid", this.finalParamUtil.getAPPID())).header("token", token)).header("userid", encryptUserid)).body(jsonParams).execute().body();
        System.out.println("testRestful()：" + data);
        return data;
    }

    public String sendAPIByAddPerson(String api, String token, String encryptUserid, String jsonParams) {
        String data = ((HttpRequest)((HttpRequest)((HttpRequest)HttpRequest.get(api).header("appid", this.finalParamUtil.getAPPID())).header("token", token)).header("userid", encryptUserid)).body(jsonParams).execute().body();
        System.out.println("testRestful()：" + data);
        return data;
    }
}
