package cn.iocoder.yudao.module.system.util.device;

import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.module.system.enums.device.SleepthingApiConstants;
import cn.iocoder.yudao.module.system.util.http.HttpClientUtil;
import cn.iocoder.yudao.module.system.util.SignUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_STRING_TIME;

public class SleepHttpSend {

    private static final Logger GM_LOG = LoggerFactory.getLogger(SleepHttpSend.class.getName());

    public static JSONObject execute(Map<String, Object> paramsMap, String method, String request) {
        // 返回json
        JSONObject jsonObj = null;
        if (request.equals("post")) {
            jsonObj = doPost(SleepthingApiConstants.PORT + ":" + SleepthingApiConstants.PORT + method, paramsMap);
        } else {
            jsonObj = doGet(SleepthingApiConstants.PORT + ":" + SleepthingApiConstants.PORT + method, paramsMap);
        }
        GM_LOG.info("返回结果：" + JSON.toJSONString(jsonObj));
        return jsonObj;

    }

    public static JSONObject doGet(String url, Map<String, Object> map) {
        String result = HttpClientUtil.doGet(url + getSignature(), map);
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = JSONObject.parseObject(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }

    public static JSONObject doPost(String url, Map<String, Object> map) {
        String json = JSON.toJSONString(map);
        HttpClient client = new HttpClient();
        client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        JSONObject jsonObject = new JSONObject();
        PostMethod method = new PostMethod(url + getSignature());
        try {
            RequestEntity entity = new StringRequestEntity(json, "application/json", "UTF-8");
            method.setRequestEntity(entity);
            client.executeMethod(method);
            InputStream inputStream = method.getResponseBodyAsStream();
            String result = IOUtils.toString(inputStream, "UTF-8");
            jsonObject = JSONObject.parseObject(result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            method.releaseConnection();
        }
        return jsonObject;
    }

    public static String getSignature() {
        String params = "?format=JSON&version=2021-07-09&signature={0}&signatureMethod=HMAC-SHA1" +
                "&signatureVersion=1.0&signatureNonce={1}&timestamp={2}&clientID={3}";
        String signatureNonce = RandomStringUtils.randomAlphanumeric(20);
        String signature = SignUtil.genHMAC(signatureNonce, SleepthingApiConstants.SECRET);
        String timestamp = DateUtils.dateTimeNow(FORMAT_STRING_TIME);
        String clientID = SleepthingApiConstants.APPID;
        params = MessageFormat.format(params, signature, signatureNonce, timestamp, clientID);
        return params;
    }


}
