package easydo.technology.core.qdplat;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import zmo.technology.lite.encrypt.ZeroEncrypt;
import zmo.technology.lite.httpcli.ZeroHttpClient;

public class QDPlatRequest {

    public static final long TEST_PROJECT_ID = 1690231135754244L;

    /// test

    public static final String AES_KEY = "46CC1C4D1C474A8029EAB203541510";
    public static final String APPID = "qdzhgdcs20210713";
    public static final String SECRET = "a45685faacfa7ac7925f3af093d334e8";
    public static final String ADDRESS = "http://39.106.50.195:8100";

    /// prod

    // public static final String APPID = "68432d7035ff8ac8";
    // public static final String SECRET = "867f745ef03a71ceadf87aca4d28f41a";
    // public static final String ADDRESS = "http://47.95.217.172:8100";

    // uri

    public static final String TCM_BASIC = "/api/provide/service/towerCrane/baseInfo";
    public static final String TCM_STATUS = "/api/provide/service/towerCrane/device";
    public static final String TCM_RECORD = "/api/provide/service/towerCrane/supervision";
    public static final String TCM_WARN = "/api/provide/service/towerCrane/alarmData";
    public static final String TCM_OVERTURN_WARN = "/api/provide/service/towerCrane/fqfData";

    public static String AES(String value) throws Exception {
        return ZeroEncrypt.encryptAESPKCS5Padding(AES_KEY, value);
    }

    private String hostname;
    private String uri;
    private String appid;
    private String secret;
    private String projectId;

    private QDPlatRequest(String hostname) {
        super();
        this.hostname = hostname;
        this.appid = APPID;
        this.secret = SECRET;
    }

    public static QDPlatRequest host(String hostname) {
        return new QDPlatRequest(hostname);
    }

    public QDPlatRequest uri(String uri) {
        this.uri = uri;
        return this;
    }

    public QDPlatRequest appid(String appid) {
        this.appid = appid;
        return this;
    }

    public QDPlatRequest secret(String secret) {
        this.secret = secret;
        return this;
    }

    public QDPlatRequest projectId(String projectId) {
        this.projectId = projectId;
        return this;
    }

    public JSONObject post(Object o) throws Exception {
        String params = "appid=" + appid + "&projectId=" + projectId + "&sign="
                + ZeroEncrypt.MD5(secret + "appid" + appid + "projectId" + projectId + secret);
        return ZeroHttpClient.request(hostname + uri + "?" + params).jsonPostObject(o).json();
    }

    public JSONObject postArray(Object[] os) throws Exception {
        String params = "appid=" + appid + "&projectId=" + projectId + "&sign="
                + ZeroEncrypt.MD5(secret + "appid" + appid + "projectId" + projectId + secret);
        return ZeroHttpClient.request(hostname + uri + "?" + params).jsonPost(JSONArray.toJSONString(os)).json();
    }

    public JSONObject postArray(Object o) throws Exception {
        return postArray(new Object[] { o });
    }
}
