package com.xxl.job.executor.util;

import com.alibaba.fastjson.JSON;
import okhttp3.*;
import okhttp3.FormBody.Builder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;

public class HttpUtil {
    static final Logger logger = LogManager.getLogger(HttpUtil.class.getName());

    public static String post(String url, Map<String, Object> data) {
        RequestBody requestBody = RequestBody.create(MediaType.get("application/json"), JSON.toJSONString(data));
        Request request = new Request.Builder().url(url).post(requestBody).build();
        Response response = null;
        try {
            OkHttpClient httpClient = new OkHttpClient();
            Call call = httpClient.newCall(request);
            response = call.execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (Exception e) {
            logger.debug(String.format("URL为 %s 的请求超时", url));
//            logger.error("USE PROXY");
//            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10809));
//            OkHttpClient.Builder builder = new OkHttpClient.Builder();
//            builder.connectTimeout(10, TimeUnit.SECONDS);
//            builder.proxy(proxy);
//            OkHttpClient httpClient = builder.build();
//            Call call = httpClient.newCall(request);
//            try {
//                response = call.execute();
//                if (response.isSuccessful()) {
//                    return response.body().string();
//                }
//            } catch (IOException ioException) {
//                ioException.printStackTrace();
//            }
        } finally {
            if (response != null) {
                response.body().close();
                response.close();
            }
        }
        return null;
    }

    public static String put(String url, Map<String, Object> data) {

        RequestBody requestBody = RequestBody.create(MediaType.get("application/json"), JSON.toJSONString(data));
        Request request = new Request.Builder().url(url).method("PUT", requestBody).build();
        Response response = null;
        try {
            OkHttpClient httpClient = new OkHttpClient();
            Call call = httpClient.newCall(request);
            response = call.execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (Exception e) {
            logger.debug(String.format("URL为 %s 的请求超时", url));
//            logger.error("USE PROXY");
//            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10809));
//            OkHttpClient.Builder builder = new OkHttpClient.Builder();
//            builder.connectTimeout(10, TimeUnit.SECONDS);
//            builder.proxy(proxy);
//            OkHttpClient httpClient = builder.build();
//            Call call = httpClient.newCall(request);
//            try {
//                response = call.execute();
//                if (response.isSuccessful()) {
//                    return response.body().string();
//                }
//            } catch (IOException ioException) {
//                ioException.printStackTrace();
//            }
        } finally {
            if (response != null) {
                response.body().close();
                response.close();
            }
        }
        return null;
    }

    public static String formPost(String url, Map<String, String> data) {
        Response response = null;
        try {
            OkHttpClient httpClient = new OkHttpClient();
            Builder builder = new FormBody.Builder();
            for (String key : data.keySet()) {
                builder.add(key, data.get(key));
            }
            Request request = new Request.Builder().url(url).post(builder.build()).build();
            Call call = httpClient.newCall(request);
            response = call.execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (Exception e) {
            logger.debug(String.format("URL为 %s 的请求超时", url));
//            logger.error("USE PROXY");
//            Builder builder = new FormBody.Builder();
//            for (String key : data.keySet()) {
//                builder.add(key, data.get(key));
//            }
//            Request request = new Request.Builder().url(url).post(builder.build()).build();
//            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10809));
//            OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
//            clientBuilder.connectTimeout(10, TimeUnit.SECONDS);
//            clientBuilder.proxy(proxy);
//            OkHttpClient httpClient = clientBuilder.build();
//            Call call = httpClient.newCall(request);
//            try {
//                response = call.execute();
//                if (response.isSuccessful()) {
//                    return response.body().string();
//                }
//            } catch (IOException ioException) {
//                ioException.printStackTrace();
//            }
        } finally {
            if (response != null) {
                response.body().close();
                response.close();
            }
        }
        return null;
    }

    public static String get(String url) {
        Request request = new Request.Builder().url(url).build();
        Response response = null;
        try {
            OkHttpClient httpClient = new OkHttpClient();
            response = httpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (Exception e) {
            logger.debug(String.format("URL为 %s 的请求超时", url));
//            logger.error("USE PROXY");
//            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10809));
//            OkHttpClient.Builder builder = new OkHttpClient.Builder();
//            builder.connectTimeout(10, TimeUnit.SECONDS);
//            builder.proxy(proxy);
//            OkHttpClient httpClient = builder.build();
//            Call call = httpClient.newCall(request);
//            try {
//                response = call.execute();
//                if (response.isSuccessful()) {
//                    return response.body().string();
//                }
//            } catch (IOException ioException) {
//                ioException.printStackTrace();
//            }
        } finally {
            if (response != null) {
                response.body().close();
                response.close();
            }
        }
        return null;
    }
}

