package com.rc.httpcore;

import com.rc.httpcore.vo.request.FaceCompare1Req;

import java.io.File;
import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttpUtils {

    private static final OkHttpClient client = HttpClient.INSTANCE.buildHttpClient();
    private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

    private OkHttpUtils() {
    }

    // 同步 GET 请求
    public static Response getSync(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);
        return call.execute();
    }

    // 异步 GET 请求
    public static Call get(String url, Callback callback) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
        return call;
    }

    // 同步 POST 请求
    public static Response postSync(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = client.newCall(request);
        return call.execute();
    }

    // 异步 POST 请求
    public static Call post(String url, String json, Callback callback) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Call call = client.newCall(request);
        call.enqueue(callback);
        return call;
    }

    public static Response postFileAndJson(String url, File file, String subjectId, FaceCompare1Req faceCompare1Req) throws IOException {
        RequestBody fileRequestBody = RequestBody.create(MultipartBody.FORM, file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), fileRequestBody);
        List<String> doorIds = faceCompare1Req.getDoorIds();
        StringBuilder doorIdsSb = new StringBuilder();
        for (int i = 0; i < doorIds.size(); i++) {
            if (i > 0) doorIdsSb.append(",");
            doorIdsSb.append(doorIds.get(i));
        }
        String doorIdsSbs = doorIdsSb.toString();
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addPart(filePart)
                .addFormDataPart("doorIds", doorIdsSbs)
                .addFormDataPart("subId", subjectId)
                .build();

        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }
}