package com.jsowell.wxpay.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class HttpUtils {

	private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

	private static final ObjectMapper JSON = new ObjectMapper();

	/**
	 * get方法
	 *
	 * @param url
	 * @return
	 */
	public static JsonNode doGet(String url) {
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		HttpGet httpget = new HttpGet(url);
		httpget.addHeader("Content-Type", "application/json;charset=UTF-8");
		httpget.addHeader("Accept", "application/json");
		try {
			String token = WechatPayUtils.getToken("GET", new URL(url), "");
			httpget.addHeader("Authorization", token);
			CloseableHttpResponse httpResponse = httpClient.execute(httpget);
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				String jsonResult = EntityUtils.toString(httpResponse.getEntity());
				return JSON.readTree(jsonResult);
			} else {
				log.warn(EntityUtils.toString(httpResponse.getEntity()));
			}
		} catch (Exception e) {
			log.error("HttpUtils.doGet error", e);
		} finally {
			try {
				httpClient.close();
			} catch (Exception e) {
				log.error("httpClient.close() error", e);
			}
		}
		return null;
	}

	/**
	 * 封装post
	 *
	 * @return
	 */
	public static Map<String, Object> doPost(String url, String body) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader("Content-Type", "application/json;chartset=utf-8");
		httpPost.addHeader("Accept", "application/json");
		try {
			String token = WechatPayUtils.getToken("POST", new URL(url), body);
			httpPost.addHeader("Authorization", token);
			if (body == null) {
				throw new IllegalArgumentException("data参数不能为空");
			}
			StringEntity stringEntity = new StringEntity(body, "utf-8");
			httpPost.setEntity(stringEntity);

			CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
			HttpEntity httpEntity = httpResponse.getEntity();

			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				String jsonResult = EntityUtils.toString(httpEntity);
				return JSON.readValue(jsonResult, HashMap.class);
			} else {
				log.warn("微信支付错误信息" + EntityUtils.toString(httpEntity));
			}
		} catch (Exception e) {
			log.error("HttpUtils.doPost error", e);
		} finally {
			try {
				httpClient.close();
			} catch (Exception e) {
				log.error("httpClient.close() error", e);
			}
		}
		return null;
	}

	/**
	 * 封装post
	 *
	 * @return
	 */
	public static Map<String, Object> doPostWexin(String url, String body) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		httpPost.addHeader("Content-Type", "application/json;chartset=utf-8");
		httpPost.addHeader("Accept", "application/json");
		try {
			String token = WechatPayUtils.getToken("POST", new URL(url), body);
			httpPost.addHeader("Authorization", token);
			if (body == null) {
				throw new IllegalArgumentException("data参数不能为空");
			}
			StringEntity stringEntity = new StringEntity(body, "utf-8");
			httpPost.setEntity(stringEntity);
			CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
			HttpEntity httpEntity = httpResponse.getEntity();
			if (httpResponse.getStatusLine().getStatusCode() == 200) {
				String jsonResult = EntityUtils.toString(httpEntity);
				return JSON.readValue(jsonResult, HashMap.class);
			} else {
				log.error("微信支付错误信息:{}", EntityUtils.toString(httpEntity));
			}
		} catch (Exception e) {
			log.error("HttpUtils.doPostWexin error", e);
		} finally {
			try {
				httpClient.close();
			} catch (Exception e) {
				log.error("httpClient.close() error", e);
			}
		}
		return null;
	}
}
