package org.springblade.openinterface.carbarrier.utils;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import org.apache.poi.ss.formula.functions.T;
import org.springblade.community.car.entity.Car;
import org.springblade.community.parkinglot.entity.ParkingLot;
import org.springblade.community.parkingveh.entity.ParkingVeh;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.openinterface.carbarrier.constant.Constants;
import org.springblade.openinterface.carbarrier.entity.*;
import org.springblade.smartapplication.carenterrecord.entity.CarEnterRecord;
import org.springblade.smartapplication.caroutrecord.entity.CarOutRecord;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created By lidexi in 2021/1/4
 * 调用安快平台接口工具类
 **/
public class HttpUtil {

	/********************************  查询类   **********************************/


	/**
	 *获取应用关联的所有停车场信息
	 */
	public static List<ParkingLot> getParkInfo(ParkInfoQuery parkInfoQuery){
		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json");
		httpHeaders.add(HttpHeaders.HOST,Constants.HOST);
        Map<String,Object> paramMap = MD5Util.getRequestMap(parkInfoQuery);
		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(paramMap), httpHeaders);
		//  执行HTTP请求
		ResponseEntity response = null;
		URI uri = URI.create(Constants.ALLPARKINFO);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {};
		response = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
//
		String map = response.getBody().toString();
		JSONObject jsonObject = new JSONObject(map);
		String data=jsonObject.get("data").toString();
		List<ParkingLot> parkingLots = JSON.parseArray(data, ParkingLot.class);

		return parkingLots;
	}

	/**
	 *获取入场记录
	 */
	public static List<CarEnterRecord> getEnterRecordResult(RecordQuery recordQuery){

		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json");
//		httpHeaders.add(HttpHeaders.HOST,Constants.HOST);
		Map<String,Object> paramMap = MD5Util.getRequestMap(recordQuery);
		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(paramMap), httpHeaders);
		//  执行HTTP请求
		ResponseEntity response = null;
		URI uri = URI.create(Constants.GETENTERCAR);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {};
		response = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
		String map = response.getBody().toString();
		JSONObject jsonObject = new JSONObject(map);
		String data=jsonObject.get("data").toString();
		List<CarEnterRecord> carEnterRecords = JSON.parseArray(data, CarEnterRecord.class);

		return carEnterRecords;
	}

	/**
	 *获取出场记录
	 */
	public static List<CarOutRecord> getOutRecordResult(RecordQuery recordQuery){

		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json");
//		httpHeaders.add(HttpHeaders.HOST,Constants.HOST);
		Map<String,Object> paramMap = MD5Util.getRequestMap(recordQuery);
		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(paramMap), httpHeaders);
		//  执行HTTP请求
		ResponseEntity response = null;
		URI uri = URI.create(Constants.GETOUTCAR);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {};
		response = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
		String map = response.getBody().toString();
		JSONObject jsonObject = new JSONObject(map);
		String data=jsonObject.get("data").toString();
		List<CarOutRecord> carEnterRecords = JSON.parseArray(data, CarOutRecord.class);
		return carEnterRecords;
	}

	/**
	 *获取停车场剩余车位
	 */
	public static ParkRemainResult getRemainSpaceResult(ParkInfoQuery parkInfoQuery){

		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json");
		httpHeaders.add(HttpHeaders.HOST,Constants.HOST);
		Map<String,Object> paramMap = MD5Util.getRequestMap(parkInfoQuery);
		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(paramMap), httpHeaders);
		//  执行HTTP请求
		ResponseEntity response = null;
		URI uri = URI.create(Constants.GETREMAINSPACE);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {};
		response = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
		String map = response.getBody().toString();
		JSONObject jsonObject = new JSONObject(map);
		String data=jsonObject.get("data").toString();
		ParkRemainResult parkRemainResult = JSON.parseObject(data,ParkRemainResult.class);
		return parkRemainResult;
	}

	/**
	 *获取停车场车道信息
	 */
	public static List<ParkingVeh> getParkVehInfo(ParkInfoQuery parkInfoQuery){

		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json");
		httpHeaders.add(HttpHeaders.HOST,Constants.HOST);
		Map<String,Object> paramMap = MD5Util.getRequestMap(parkInfoQuery);
		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(paramMap), httpHeaders);
		//  执行HTTP请求
		ResponseEntity response = null;
		URI uri = URI.create(Constants.GETPARKVEHINFO);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {};
		response = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
		String map = response.getBody().toString();
		JSONObject jsonObject = new JSONObject(map);
		String data=jsonObject.get("data").toString();
		List<ParkingVeh> parkingVehs = JSON.parseArray(data,ParkingVeh.class);
		return parkingVehs;
	}


	/********************************  查阅类   **********************************/


	/**
	 *远程开关闸
	 */
	public static String openOrClose(OpenOrCloseGateQuery openOrCloseGateQuery){

		RestTemplate restTemplate = new RestTemplate();
		HttpHeaders httpHeaders = new HttpHeaders();
		httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json");
//		httpHeaders.add(HttpHeaders.HOST,Constants.HOST);
		Map<String,Object> paramMap = MD5Util.getRequestMap(openOrCloseGateQuery);
		HttpEntity<String> requestEntity = new HttpEntity<String>(JSONUtil.toJsonStr(paramMap), httpHeaders);
		//  执行HTTP请求
		ResponseEntity response = null;
		URI uri = null;
		if(openOrCloseGateQuery.getOpenOrClose() == 0 ){//开闸
			uri = URI.create(Constants.OPENGATE);
		}else {
			uri = URI.create(Constants.CLOSEGATE);
		}
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {};
		response = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
		String map = response.getBody().toString();
		JSONObject jsonObject = new JSONObject(map);
		String msg=jsonObject.get("msg").toString();

		return msg;
	}

}
