package com.tofly.qualitycontrol.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Maps;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 调用第三方接口工具类
 */
public class CallAPIUtil {

	/**
	 * 调用第三方的接口方法
	 *
	 * @param object 第三方接口规定条件实体类
	 * @param url 调用的目标接口地址
	 * @return
	 * @throws Exception
	 */
	public final static Map<String,Object> callAPIUtil(Object object,String ip,String url) throws Exception {

		Map<String,Object> map = Maps.newHashMap();
		Map<String,Object> objectMap = JSONObject.parseObject(JSON.toJSONString(object),new TypeReference<Map<String,Object>>() {
		});
		//由于参数加密是指对所有需要传的参数都要加密，所以需要加上这个 SING 参数
		objectMap.put("SING","");
		//获取签名时的必要参数：Domain
		map.put("Domain",objectMap);
		//转json字符串
		String string = JSON.toJSONString(map);
		JSONObject json = (JSONObject) JSONObject.parse(string);
		//获取签名
		String sing = getSing(ip,json);
		//设置获取后的签名
		objectMap.put("SING",sing);
		//转json字符串
		String info = JSON.toJSONString(objectMap);
		JSONObject jsonData = (JSONObject) JSONObject.parse(info);
		//请求目标接口
		Map<String,Object> data = callAPI(jsonData,ip,url);

		return data;
	}

	/**
	 * 获取签名
	 * 不用调用
	 *
	 * @param date
	 * @return
	 * @throws Exception
	 */
	private static String getSing(String ip,JSONObject date) throws Exception {
		HttpClient client = HttpClients.createDefault();
		// 要调用的接口方法
		String url = ip+"/api/SignHelper/Sign";
		//建立HttpPost对象
		HttpPost post = new HttpPost(url);
		JSONObject jsonObject = null;
		StringEntity s = new StringEntity(date.toString(),StandardCharsets.UTF_8);
		post.addHeader("Content-Type","application/json");
		post.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36");
		post.addHeader("Connection","keep-alive");
		post.addHeader("Accept-Language","zh-CN,zh;q=0.9");
		post.addHeader("Accept-Encoding","gzip, deflate");
		post.addHeader("Accept","application/json");
		post.setEntity(s);
		HttpResponse res = client.execute(post);
		//得到返回的数据
		String response = EntityUtils.toString(res.getEntity());

		if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			//将返回数据转换为实体类
			Map singBean = JSONObject.parseObject(response,Map.class);
			//只返回加密的签名
			return singBean.get("message").toString();
		}
		//返回null
		return jsonObject.toString();
	}

	/**
	 * 调用目标接口
	 * 不用调用
	 *
	 * @param data
	 * @param inputUrl
	 * @return
	 * @throws Exception
	 */
	private static Map<String,Object> callAPI(JSONObject data,String ip,String inputUrl) throws Exception {
		HttpClient client = HttpClients.createDefault();
		// 要调用的接口路径
		String url = ip+inputUrl;
		//建立HttpPost对象
		HttpPost post = new HttpPost(url);
		Map<String,Object> resultMap = null;
		StringEntity s = new StringEntity(data.toString(),StandardCharsets.UTF_8);
		post.addHeader("Content-Type","application/json");
		post.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36");
		post.addHeader("Connection","keep-alive");
		post.addHeader("Accept-Language","zh-CN,zh;q=0.9");
		post.addHeader("Accept-Encoding","gzip, deflate");
		post.addHeader("Accept","application/json");
		post.setEntity(s);
		HttpResponse res = client.execute(post);
		//得到返回的数据
		String response = EntityUtils.toString(res.getEntity());
		if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			//将返回数据转换
			resultMap = JSONObject.parseObject(response,new TypeReference<Map<String,Object>>() {
			});
			//返回数据
			return resultMap;
		}
		//返回null
		return resultMap;
	}

}

