package com.jeequan.jeepay.thirdparty.channel.yspay.utils;


import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.util.Map;

public class HttpRequestUtil {

	/**
	 * 图片上传接口，未校验文件名称，商户进件需要用到
	 * @param url
	 * @param paramMap
	 * @param file
	 * @return
	 * @throws Exception
	 */
	public static String sendPostFile(String url, Map<String,String> paramMap, File file) throws Exception {
		HttpPost httpPost = new HttpPost(url);
		//如果返回403 增加一下代码模拟浏览器
		//httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0");;

		CloseableHttpClient client = HttpClientBuilder.create().build();

		MultipartEntityBuilder entity = MultipartEntityBuilder.create()
				.setContentType(ContentType.MULTIPART_FORM_DATA)
				.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
				.addBinaryBody("file", new FileInputStream(file), ContentType.DEFAULT_BINARY, file.getName()) //uploadFile对应服务端类的同名属性<File类型>
				.setCharset(Charset.forName("utf-8"));

		for (String key : paramMap.keySet()) {
			String value = paramMap.get(key);
			entity.addTextBody(key, value);
		}

		httpPost.setEntity(entity.build());
		HttpResponse httpResponse = client.execute(httpPost);
		if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
			System.out.println(httpResponse.getStatusLine().getStatusCode());
		}

		HttpEntity resEntity = httpResponse.getEntity();
		return null == resEntity ? "" : EntityUtils.toString(resEntity, "utf-8");
	}

	/**
	 * 文件上传 校验了文件名称，支付宝实名认证文件上传用到
	 * @param url
	 * @param paramMap
	 * @param file
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	public static String sendPostFileAndName(String url, Map<String,String> paramMap, File file,String fileName) throws Exception {
		if(StringUtils.isEmpty(fileName)){
			throw new Exception("文件名称不能为空");
		}
		HttpPost httpPost = new HttpPost(url);
		//如果返回403 增加一下代码模拟浏览器
		//httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0");;

		CloseableHttpClient client = HttpClientBuilder.create().build();

		MultipartEntityBuilder entity = MultipartEntityBuilder.create()
				.setContentType(ContentType.MULTIPART_FORM_DATA)
				.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
				.addBinaryBody("file", new FileInputStream(file), ContentType.DEFAULT_BINARY, fileName) //uploadFile对应服务端类的同名属性<File类型>
				.setCharset(Charset.forName("utf-8"));

		for (String key : paramMap.keySet()) {
			String value = paramMap.get(key);
			entity.addTextBody(key, value);
		}

		httpPost.setEntity(entity.build());
		HttpResponse httpResponse = client.execute(httpPost);
		if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
			System.out.println(httpResponse.getStatusLine().getStatusCode());
		}

		HttpEntity resEntity = httpResponse.getEntity();
		return null == resEntity ? "" : EntityUtils.toString(resEntity, "utf-8");
	}
}
