package psdi.app.iface.oa;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.http.client.ClientProtocolException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Iterator;
import java.util.Map;

public class HttpUtilOa {
    private static Logger log = LoggerFactory.getLogger(HttpUtilOa.class);

    public static JSONObject post(String url, JSONObject json, String token) throws Exception {
        log.info("调用接口 --> {} 入参-->{}", url, json.toString());
        System.out.println("start inface-->"+"url="+url+" param="+json.toString());
        HttpClient httpClient = new HttpClient();
        PostMethod httpPost = new PostMethod(url);
        httpPost.addRequestHeader("accessToken", token);
        StringRequestEntity requestEntity = new StringRequestEntity(json.toString(), "application/json",
                "UTF-8");
        httpPost.setRequestEntity(requestEntity);
        httpClient.executeMethod(httpPost);
        JSONObject feedback = getHttpEntityContent(httpPost.getResponseBodyAsStream());
        log.info("接口--> {} 返回-->{}", url, feedback.toString());
        System.out.println("return inface-->"+"url="+url+" param="+feedback.toString());
        return feedback;
    }

    private static JSONObject getHttpEntityContent(InputStream is) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String line = br.readLine();
            StringBuilder sb = new StringBuilder();
            while (line != null) {
                sb.append(line + "\n");
                line = br.readLine();
            }
            is.close();
            br.close();
            return JSONObject.parseObject(sb.toString());
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new JSONObject();
    }

    public static JSONObject post(String url, JSONObject json) throws IOException {
        log.info("调用接口 --> {} 入参-->{}", url, json.toString());
        System.out.println("start inface-->"+"url="+url+" param="+json.toString());
        HttpClient httpClient = new HttpClient();
        PostMethod httpPost = new PostMethod(url);
        StringRequestEntity requestEntity = new StringRequestEntity(json.toString(), "application/json",
                "UTF-8");
        httpPost.setRequestEntity(requestEntity);
        httpClient.executeMethod(httpPost);
        JSONObject feedback = getHttpEntityContent(httpPost.getResponseBodyAsStream());
        log.info("接口--> {} 返回-->{}", url, feedback.toString());
        System.out.println("return inface-->"+"url="+url+" param="+feedback.toString());
        return feedback;
    }

    public static JSONObject getWithHeaders(String url, Map<String, String> queryParams, Map<String, String> headerParams) throws IOException {
//        log.info("调用接口 --> {} 入参-->{}", url, queryParams.toString());
        HttpClient httpClient = new HttpClient();
        GetMethod getMethod = new GetMethod(url);
        if (headerParams != null) {
            Iterator<String> iterator = headerParams.keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
                getMethod.addRequestHeader(key, headerParams.get(key));
            }
        }
        NameValuePair[] nameValuePairs = new NameValuePair[queryParams.entrySet().size()];
        Iterator<String> iterator = queryParams.keySet().iterator();
        int i = 0;
        while (iterator.hasNext()) {
            String key = iterator.next();
            nameValuePairs[i] = new NameValuePair(key, queryParams.get(key));
            i++;
        }

        getMethod.setQueryString(nameValuePairs);
        httpClient.executeMethod(getMethod);
        JSONObject feedback = getHttpEntityContent(getMethod.getResponseBodyAsStream());
//        log.info("接口--> {} 返回-->{}", url, feedback.toString());
        return feedback;
    }


    public static JSONObject postWithHeader(String url, JSONObject json, Map<String, String> headerParams) throws IOException {
//        log.info("调用接口 --> {} 入参-->{}", url, json.toString());
        HttpClient httpClient = new HttpClient();
        PostMethod httpPost = new PostMethod(url);
        Iterator<String> iterator = headerParams.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            httpPost.addRequestHeader(key, headerParams.get(key));
        }
        StringRequestEntity requestEntity = new StringRequestEntity(json.toString(), "application/json",
                "UTF-8");
        httpPost.setRequestEntity(requestEntity);
        httpClient.executeMethod(httpPost);
        JSONObject feedback = getHttpEntityContent(httpPost.getResponseBodyAsStream());
        log.info("接口--> {} 返回-->{}", url, feedback.toString());
        return feedback;
    }

}
