package org.dromara.resource.danmi_sms.common;

import org.apache.commons.codec.digest.DigestUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;

public class HttpUtil {
  public static String createCommonParam(String sid, String token) {
    long timestamp = System.currentTimeMillis();
    String sig = DigestUtils.md5Hex(sid + token + timestamp);
    return "&timestamp=" + timestamp + "&sig=" + sig + "&respDataType=1";
  }

  public static String post(String url, String body) {
    System.out.println("body:" + System.lineSeparator() + body);
    String result = "";
    try {
      OutputStreamWriter out = null;
      BufferedReader in = null;
      URL realUrl = new URL(url);
      URLConnection conn = realUrl.openConnection();
      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.setConnectTimeout(5000);
      conn.setReadTimeout(20000);
      conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
      out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
      out.write(body);
      out.flush();
      in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
      String line = "";
      boolean firstLine = true;
      while ((line = in.readLine()) != null) {
        if (firstLine) {
          firstLine = false;
        } else {
          result = result + System.lineSeparator();
        }
        result = result + line;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }

  public static String postHuiDiao(String url, String body) {
    String result = "";
    try {
      OutputStreamWriter out = null;
      BufferedReader in = null;
      URL realUrl = new URL(url);
      URLConnection conn = realUrl.openConnection();
      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.setConnectTimeout(5000);
      conn.setReadTimeout(20000);
      out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
      out.write(body);
      out.flush();
      in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
      String line = "";
      boolean firstLine = true;
      while ((line = in.readLine()) != null) {
        if (firstLine) {
          firstLine = false;
        } else {
          result = result + System.lineSeparator();
        }
        result = result + line;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return result;
  }

  public static String createParamWithTime(String sid, String token, Long timestamp) {
    String sig = DigestUtils.md5Hex(sid + token + timestamp);
    return "&timestamp=" + timestamp + "&sig=" + sig + "&respDataType=UTF-8";
  }
}
