package com.prolog.eis.utils;

import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.prolog.eis.common.util.WcsResult;
import com.prolog.framework.common.message.RestMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Author
 * @Description http工具
 * @CreateTime 2020-10-29 13:36
 */
@Component
public class RestHttpUtils {

	private Logger logger = LoggerFactory.getLogger(RestHttpUtils.class);

	@Autowired
	private RestTemplate newRestTemplate;

	public RestHttpUtils(RestTemplate restTemplate) {
		newRestTemplate = restTemplate;
	}

	public cn.hutool.json.JSONObject post(String url, Object params)throws IOException  {
		HttpEntity<Object> entity = this.parseParams(params);
		System.out.println(entity.toString());
		cn.hutool.json.JSONObject data = this.newRestTemplate.postForObject(url,entity, JSONObject.class);
		logger.info("EIS <- WCS Resutl: {}",data.toString());
		return data;
	}

	public <T> WcsResult<T> postWcs(String url, Object params, TypeReference<WcsResult<T>> typeReference) throws IOException {
		HttpEntity<Object> entity = this.parseParams(params);
		System.out.println(entity.toString());
		WcsResult data = this.newRestTemplate.postForObject(url,entity,WcsResult.class);
		logger.info("EIS <- WCS Resutl: {}",data.toString());
		return data;
	}


	public <T> RestMessage<T> post(String url, Object params, TypeReference<RestMessage<T>> typeReference) throws IOException {
		HttpEntity<Object> entity = this.parseParams(params);
		String data = newRestTemplate.postForObject(url, entity, String.class, new Object[0]);
		this.logger.info("EIS <- WCS Resutl: {}", data);
		RestMessage<T> result = RestMessage.parseJsonString(data, typeReference);
		return result;
	}

	public <T> RestMessage<T> postWms(String url, Object params, TypeReference<RestMessage<T>> typeReference) throws IOException {
		HttpEntity<Object> entity = this.parseParams(params);
		String data = newRestTemplate.postForObject(url, entity, String.class);
		this.logger.info("WMS <- EIS Resutl: {}", data);
		RestMessage<T> result = RestMessage.parseJsonString(data, typeReference);
		return result;
	}

	private HttpEntity<Object> parseParams(Object params) throws JsonProcessingException {
		newRestTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8)); // 支持中文编码
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
		HttpEntity<Object> entity = new HttpEntity<Object>(params, headers);
		return entity;
	}

}
