package com.neusoft.smart.pos.order.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtils {

    public static final String CONTENT_TYPE_XML = "application/xml";
    public static final String DEFAULT_CONTENT_TYPE = "application/json";
    public static final String DEFAULT_CHARSET = "UTF-8";

    public static String postXMLData(String url, String data){
        return postData(url,data,CONTENT_TYPE_XML);
    }

    public static String postData(String url, String data){
        return postData(url,data,DEFAULT_CONTENT_TYPE);
    }

    /**
     * 发送xml格式数据请求方法
     * @param url
     * @param data
     * @return
     */
    public static String postData(String url, String data,String contentType){
        HttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        StringEntity requestEntity = new StringEntity(data, DEFAULT_CHARSET);
        httpPost.addHeader("Content-Type", contentType);
        httpPost.setEntity(requestEntity);
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity responseEntity = response.getEntity();
            result = EntityUtils.toString(responseEntity, DEFAULT_CHARSET);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据request对象获取数据信息
     * @param request
     * @return
     */
    public static String getStringDataFromRequest(HttpServletRequest request){
        String dataString = null;
        try {
            BufferedReader reader = request.getReader();
            String line;
            StringBuffer inputString = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                inputString.append(line);
            }
            dataString = inputString.toString();
            request.getReader().close();
        } catch (IOException e) {
            e.printStackTrace();
            return dataString;
        }
        return dataString;
    }

    public static String putData(String url, String data){
        return putData(url,data,DEFAULT_CONTENT_TYPE);
    }

    /**
     * 发送xml格式数据请求方法
     * @param url
     * @param data
     * @return
     */
    public static String putData(String url, String data,String contentType){
        HttpClient httpclient = HttpClients.createDefault();
        HttpPut httpPut = new HttpPut(url);
        StringEntity requestEntity = new StringEntity(data, DEFAULT_CHARSET);
        httpPut.addHeader("Content-Type", contentType);
        httpPut.setEntity(requestEntity);
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httpPut);
            HttpEntity responseEntity = response.getEntity();
            result = EntityUtils.toString(responseEntity, DEFAULT_CHARSET);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String deleteData(String url, Map<String, String> params){
        return deleteData(url,params,DEFAULT_CONTENT_TYPE);
    }

    /**
     * 发送xml格式数据请求方法
     * @param url
     * @return
     */
    public static String deleteData(String url, Map<String, String> params,String contentType){
        HttpClient httpclient = HttpClients.createDefault();
        List<NameValuePair> qparams = getParamsList(params);
        if (qparams != null && qparams.size() > 0) {
            String formatParams = URLEncodedUtils.format(qparams, DEFAULT_CHARSET);
            url = (url.indexOf("?")) < 0 ? (url + "?" + formatParams)
                    : (url.substring(0, url.indexOf("?") + 1) + formatParams);
        }
        HttpDelete httpDelete = new HttpDelete(url);
        httpDelete.addHeader("Content-Type", contentType);
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httpDelete);
            HttpEntity responseEntity = response.getEntity();
            result = EntityUtils.toString(responseEntity, DEFAULT_CHARSET);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getData(String url){
        return getData(url,DEFAULT_CONTENT_TYPE);
    }

    /**
     * 发送xml格式数据请求方法
     * @param url
     * @return
     */
    public static String getData(String url,String contentType){
        HttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader("Content-Type", contentType);
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httpGet);
            HttpEntity responseEntity = response.getEntity();
            result = EntityUtils.toString(responseEntity, DEFAULT_CHARSET);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    private static List<NameValuePair> getParamsList(Map<String, String> paramsMap) {
        if (paramsMap == null || paramsMap.size() == 0) {
            return null;
        }
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        for (Map.Entry<String, String> map : paramsMap.entrySet()) {
            params.add(new BasicNameValuePair(map.getKey(), map.getValue()));
        }
        return params;
    }
}
