package com.liquidnet.commons.lang.util;

import com.liquidnet.commons.lang.util.spring.RestTemplateConfig;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

public class HttpUtil {
    private static RestTemplate restTemplate;

    static {
        restTemplate = RestTemplateConfig.getRestTemplate();
    }

    /**
     * get请求
     *
     * @param url
     * @param params 请求参数
     * @return
     */
    public static String get(String url, MultiValueMap<String, String> params) {
        return get(url, params, null);
    }

    /**
     * get请求
     *
     * @param url
     * @param params  请求参数
     * @param headers 请求头
     * @return
     */
    public static String get(String url, MultiValueMap<String, String> params, MultiValueMap<String, String> headers) {
        return request(url, params, headers, HttpMethod.GET);
    }

    /**
     * post请求
     *
     * @param url
     * @param params 请求参数
     * @return
     */
    public static String post(String url, MultiValueMap<String, String> params) {
        return post(url, params, null);
    }

    /**
     * post请求
     *
     * @param url
     * @param params  请求参数
     * @param headers 请求头
     * @return
     */
    public static String post(String url, MultiValueMap<String, String> params, MultiValueMap<String, String> headers) {
        return request(url, params, headers, HttpMethod.POST);
    }

    /**
     * post请求
     *
     * @param url
     * @param jsonString 请求参数
     * @param headers    请求头
     * @return
     */
    public static String postRaw(String url, String jsonString, MultiValueMap<String, String> headers) {
        return requestRaw(url, jsonString, headers, HttpMethod.POST);
    }

    /**
     * put请求
     *
     * @param url
     * @param params 请求参数
     * @return
     */
    public static String put(String url, MultiValueMap<String, String> params) {
        return put(url, params, null);
    }

    /**
     * put请求
     *
     * @param url
     * @param params  请求参数
     * @param headers 请求头
     * @return
     */
    public static String put(String url, MultiValueMap<String, String> params, MultiValueMap<String, String> headers) {
        return request(url, params, headers, HttpMethod.PUT);
    }

    /**
     * delete请求
     *
     * @param url
     * @param params 请求参数
     * @return
     */
    public static String delete(String url, MultiValueMap<String, String> params) {
        return delete(url, params, null);
    }

    /**
     * delete请求
     *
     * @param url
     * @param params  请求参数
     * @param headers 请求头
     * @return
     */
    public static String delete(String url, MultiValueMap<String, String> params, MultiValueMap<String, String> headers) {
        return request(url, params, headers, HttpMethod.DELETE);
    }

    /**
     * 表单请求
     *
     * @param url
     * @param params  请求参数
     * @param headers 请求头
     * @param method  请求方式
     * @return
     */
    public static String request(String url, MultiValueMap<String, String> params, MultiValueMap<String, String> headers, HttpMethod method) {
        if (params == null) {
            params = new LinkedMultiValueMap<>();
        }
        return request(url, params, headers, method, MediaType.APPLICATION_FORM_URLENCODED);
    }

    /**
     * 表单请求Raw
     *
     * @param url     请求地址
     * @param params  请求参数
     * @param headers 请求头
     * @param method  请求方式
     * @return
     */
    public static String requestRaw(String url, String params, MultiValueMap<String, String> headers, HttpMethod method) {
        if (params == null) {
            params = "";
        }
        return requestRaw(url, params, headers, method, MediaType.APPLICATION_JSON);
    }

    /**
     * http请求
     *
     * @param url
     * @param params    请求参数
     * @param headers   请求头
     * @param method    请求方式
     * @param mediaType 参数类型
     * @return
     */
    public static String request(String url, Object params, MultiValueMap<String, String> headers, HttpMethod method, MediaType mediaType) {
        if (url == null || url.trim().isEmpty()) {
            return null;
        }

        // header
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headers != null) {
            httpHeaders.addAll(headers);
        }
        HttpEntity<Object> httpEntity = new HttpEntity(params, httpHeaders);

//        RestTemplate restTemplate = new RestTemplate();
        // 提交方式：表单、json
        ResponseEntity<String> response = restTemplate.exchange(url, method, httpEntity, String.class);
        return response.getBody();
    }

    public static String requestRaw(String url, Object params, MultiValueMap<String, String> headers, HttpMethod method, MediaType mediaType) {
        if (url == null || url.trim().isEmpty()) {
            return null;
        }

        // header
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Accept", MediaType.APPLICATION_JSON.toString());
        httpHeaders.set("Content-Type","application/json;charset=UTF-8");
        if (headers != null) {
            httpHeaders.addAll(headers);
        }
        HttpEntity<Object> httpEntity = new HttpEntity(params, httpHeaders);

//        RestTemplate restTemplate = new RestTemplate();
        // 提交方式：表单、json
        ResponseEntity<String> response = restTemplate.postForEntity(url, httpEntity, String.class);
        return response.getBody();
    }


    private static final String PHP_API_KEY = "R7tXY9smPQPG9Ku5yI0u6sfnlckmk04V";

    public static String postToPhpApi(String url, MultiValueMap<String, String> params) {
        params.add("sign", processForPhpApi(params).concat("&key=").concat(PHP_API_KEY).toUpperCase());
        return post(url, params, null);
    }

    private static String processForPhpApi(MultiValueMap<String, String> map) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {
            sb.append(entry.getKey()).append("=").append(entry.getValue().get(0)).append("&");
        }
        String targetStr = sb.substring(0, sb.length() - 1);
        try {
            targetStr = URLDecoder.decode(targetStr, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        targetStr = targetStr.replace("%3D", "=").replace("%26", "&");
        return targetStr;
    }

    public static void main(String[] args) {
        String s = HttpUtil.get("www.baidu.com", null);
        System.out.println(s);
    }
}
