package cn.freemud.utils;

import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Iterator;
import java.util.Map;

/**
 * http 请求工具
 *
 * @author liming.guo
 * @date 2018/05/11
 */
@Component
public class HttpUtil {

    public static String sendPostRequest(String url, Map<String, Object> map, MediaType mediaType) {
        if (StringUtils.isBlank(url)) {
            return "";
        }
        if (mediaType == null) {
            mediaType = MediaType.APPLICATION_JSON;
        }
        RestTemplate client = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(mediaType);
        headers.add("Accept", MediaType.APPLICATION_JSON_VALUE);
        if (MediaType.APPLICATION_JSON_VALUE.equals(mediaType.toString())) {
            Gson gson = new Gson();
            String json = gson.toJson(map);
            HttpEntity<String> requestEntity = new HttpEntity<>(json, headers);
            ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
            return response.getBody();
        } else {
            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
            Iterator it = map.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Object> entry = (Map.Entry) it.next();
                params.add(entry.getKey(), entry.getValue());
            }
            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
            ResponseEntity<String> response = client.exchange(url, HttpMethod.POST, requestEntity, String.class);
            return response.getBody();
        }
    }

    public static String sendGetRequest(String url, Map<String, Object> map, MediaType mediaType) {
        if (StringUtils.isBlank(url)) {
            return "";
        }
        url = url + "?" + getParams(map);
        RestTemplate client = new RestTemplate();
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        Iterator it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry) it.next();
            params.add(entry.getKey(), entry.getValue());
        }
        return client.getForObject(url, String.class, params);

    }

    private static String getParams(Map<String, Object> map) {
        Iterator it = map.entrySet().iterator();
        StringBuffer sb = new StringBuffer();
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = (Map.Entry) it.next();
            sb.append(entry.getKey()).append("={").append(entry.getKey()).append("}&");
        }
        String str = sb.toString();
        if (str.endsWith("&")) {
            str = str.substring(0, str.length() - 1);
        }
        return str;
    }
}
