package com.blueland.core.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HttpUtils {


    private static HttpUtils instance;

    private RestTemplate restTemplate = new RestTemplate();

    private HttpUtils() {
    }

    public static HttpUtils getInstance() {
        if (null == instance) {
            instance = new HttpUtils();
        }
        return instance;
    }

    public <T> T postForObject(String url, Object requestParameter,
        Map<String, String> requestHeaders, Class<T> responseType,String authorization) {
        String result = exchange(url, HttpMethod.POST, requestParameter, requestHeaders, authorization);

        return JsonUtil.convertJsonStrToBean(result, responseType);
    }

    public <T> List<T> postForList(String url, Object requestParameter,
        Map<String, String> requestHeaders, Class<T> responseType,String authorization) {
        String result = exchange(url, HttpMethod.POST, requestParameter, requestHeaders, authorization);

        return JsonUtil.convertJsonStrToList(result, responseType);
    }

    public <T> T putForObject(String url, Object requestParameter,
        Map<String, String> requestHeaders, Class<T> responseType,String authorization) {
        String result = exchange(url, HttpMethod.PUT, requestParameter, requestHeaders, authorization);

        return JsonUtil.convertJsonStrToBean(result, responseType);
    }

    public <T> T getForObject(String url, Map<String, String> requestHeaders, Class<T> responseType,String authorization) {
        String result = exchange(url, HttpMethod.GET, null, requestHeaders, authorization);

        return JsonUtil.convertJsonStrToBean(result, responseType);
    }

    public <T> List<T> getForList(String url, Map<String, String> requestHeaders,
        Class<T> responseType,String authorization) {
        String result = exchange(url, HttpMethod.GET, null, requestHeaders, authorization);

        return JsonUtil.convertJsonStrToList(result, responseType);
    }

    private String exchange(String url, HttpMethod httpMethod, Object requestParameter,
        Map<String, String> requestHeaders,String authorization) {
    	String result = "";
        HttpHeaders httpHeaders = new HttpHeaders();
        ensureDefaultHeader(httpMethod, httpHeaders);
        if (null != requestHeaders) {
            for (Entry<String, String> entry : requestHeaders.entrySet()) {
                httpHeaders.add(entry.getKey(), entry.getValue());
            }
        }

        if(StringUtil.notEmpty(authorization)) {
        	httpHeaders.add(HttpHeaders.AUTHORIZATION, "Bearer "+ authorization);
        }
        
        HttpEntity<Object> httpEntity;
        if (null != requestParameter) {
            httpEntity = new HttpEntity<Object>(requestParameter, httpHeaders);
        } else {
            httpEntity = new HttpEntity<Object>(httpHeaders);
        }
        //System.setProperty("javax.net.debug", "all");
        System.out.println("url:"+url);
        if(url.startsWith("https://")) {
//        	log.debug("https start,url=" + url);
        	restTemplate = new RestTemplate(new HttpsClientRequestFactory());
        	result = restTemplate.exchange(url, httpMethod, httpEntity, String.class).getBody();
//        	log.debug("https end,url=" + url);
        }else {
//        	 log.debug("http start,url=" + url);
        	 restTemplate = new RestTemplate();
             result = restTemplate.exchange(url, httpMethod, httpEntity, String.class)
                 .getBody();
//             log.debug("http end,url=" + url);
        }
        
        return result;
    }

    private void ensureDefaultHeader(HttpMethod httpMethod, HttpHeaders httpHeaders) {
        if (HttpMethod.POST == httpMethod) {
            httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
        }
    }
    
    public static void main(String []args){
    	System.out.println("test");
        Map<String, String> requestHeaders= new HashMap(1);
    	requestHeaders.put(HttpHeaders.AUTHORIZATION, "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJCQ0FuMkNIRGtvU0RKdjdJcDBrMFdqVkhVUm85NElTRiJ9.zzJtS7TYaaLy-VIANMU96SOw2r9driz-0Qxy-To9ko8"); 
//        String url2 = "https://crm.vazyme.com/blueland-plateform-render/role/BIO/role/leader/users/luob";
    	String url2 = "http://47.100.56.159:38081/blueland-plateform-render/role/BIO/role/leader/users/luob";
//    	String res = getInstance().getForObject(url2,  requestHeaders, String.class,"Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJCQ0FuMkNIRGtvU0RKdjdJcDBrMFdqVkhVUm85NElTRiJ9.zzJtS7TYaaLy-VIANMU96SOw2r9driz-0Qxy-To9ko8");
    	String res = getInstance().getForObject(url2,  requestHeaders, String.class,"");
    	System.out.println(res);

    }
}
