package cn.sikey.mcdisk.config;

import cn.sikey.framework.common.exception.ServiceException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Component;

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

/**
 * @Author: qin
 * @Date: 2025/4/16
 * @Description: http工具
 */
@Slf4j
@Component
public class HttpUtil {

    @Resource
    private OkHttpClient okHttpClient;

    /**
     * GET请求
     */
    public String doGet(String url, Map<String, String> params) {
        HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
        if (params != null) {
            params.forEach(urlBuilder::addQueryParameter);
        }
        Request request = new Request.Builder()
                .url(urlBuilder.build())
                .get()
                .build();

        try (Response response = okHttpClient.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "get请求失败");
            }

            if (Objects.nonNull(response.body())) {
                return response.body().string();
            }
        } catch (IOException e) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "get请求失败");
        }
        return null;
    }

    /**
     * POST表单请求
     */
    public String doPostForm(String url, Map<String, String> formData) {
        FormBody.Builder builder = new FormBody.Builder();
        if (formData != null) {
            formData.forEach(builder::add);
        }

        Request request = new Request.Builder()
                .url(url)
                .post(builder.build())
                .build();

        try (Response response = okHttpClient.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "post请求失败");
            }

            if (Objects.nonNull(response.body())) {
                return response.body().string();
            }
        } catch (IOException e) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "post请求失败");
        }
        return null;
    }

    /**
     * POST JSON请求
     */
    public String doPostJson(String url, String json) {
        RequestBody body = RequestBody.create(
                json, MediaType.parse("application/json; charset=utf-8"));

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

        try (Response response = okHttpClient.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "post请求失败");
            }

            if (Objects.nonNull(response.body())) {
                return response.body().string();
            }
        } catch (IOException e) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "post请求失败");
        }
        return null;
    }

}
