package com.kym.common.utils;

import com.alibaba.fastjson2.JSONObject;
import okhttp3.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @author skyline
 * @description http工具
 * @date 2023-07-23 16:31
 */
@Component
public class HttpUtil {

    public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
    static OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder().build();


    public static String get(String url) {
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        return synchronizedCall(request);
    }

    /**
     * Get请求
     *
     * @param url
     * @param params
     * @return
     */
    public static JSONObject getJson(String url, Map<String, String> params) {
        var urlBuilder = HttpUrl.parse(url).newBuilder();
        params.forEach(urlBuilder::addQueryParameter);
        Request request = new Request.Builder()
                .get()
                .url(urlBuilder.build())
                .build();

        return parseJson(synchronizedCall(request));
    }

    public static <T> T get(String url, Class<T> clz) {
        return parse(get(url), clz);
    }

    public static String post(String url) {
        RequestBody requestBody = RequestBody.create("", JSON);
        Request request = new Request.Builder()
                .post(requestBody)
                .url(url)
                .build();

        return synchronizedCall(request);
    }
    public static String post(String url, String json) {
        RequestBody requestBody = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .post(requestBody)
                .url(url)
                .build();

        return synchronizedCall(request);
    }

    public static String post(String url, String json, Headers headers) {
        RequestBody requestBody = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .headers(headers)
                .post(requestBody)
                .url(url)
                .build();

        return synchronizedCall(request);
    }

    public static String post(String url, Headers headers) {
        Request request = new Request.Builder()
                .headers(headers)
                .post(okhttp3.internal.Util.EMPTY_REQUEST)
                .url(url)
                .build();

        return synchronizedCall(request);
    }

    public static <T> T post(String url, Map<String,Object> params, Class<T> clz) {

        RequestBody requestBody = RequestBody.create(JSON, JSONObject.toJSONString(params));
        Request request = new Request.Builder()
                .post(requestBody)
                .url(url)
                .build();

        return parse(synchronizedCall(request), clz);
    }

    public static <T> T post(String url, String params, Headers headers, Class<T> clz) {

        RequestBody requestBody = RequestBody.create(JSON, params);
        Request request = new Request.Builder()
                .headers(headers)
                .post(requestBody)
                .url(url)
                .build();

        return parse(synchronizedCall(request), clz);
    }


    public static <T> T post(String url, String json, Class<T> clz) {
        return parse(post(url, json), clz);
    }

    public static String put(String url, String json) {
        RequestBody body = RequestBody.create(JSON, json);

        Request request = new Request.Builder()
                .url(url)
                .put(body)
                .build();

        return synchronizedCall(request);
    }

    public static String put(String url) {
        return put(url, "");
    }


    public static <T> T put(String url, String json, Class<T> clz) {
        return parse(put(url, json), clz);
    }


    public static String delete(String url, String json) {
        RequestBody body = RequestBody.create(JSON, json);

        Request request = new Request.Builder()
                .url(url)
                .delete(body)
                .build();
        return synchronizedCall(request);
    }

    public static String delete(String url) {
        return delete(url, "");
    }

    public static <T> T delete(String url, Class<T> clz) {
        return parse(
                delete(url, ""),
                clz
        );
    }

    public static <T> T delete(String url, String json, Class<T> clz) {
        return parse(delete(url, json), clz);
    }

    private static String synchronizedCall(Request request) {
        try (Response response = HTTP_CLIENT.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static <T> T synchronizedCall(Request request, Class<T> clz) {
        return parse(synchronizedCall(request), clz);
    }

    public static <T> T parse(String json, Class<T> clz) {
        return JSONObject.parseObject(json, clz);
    }

    public static JSONObject parseJson(String json) {
        return JSONObject.parseObject(json);
    }


}
