package com.gallery.free.wallpaper.environment.hy;

import android.util.Log;

import com.google.gson.JsonObject;

import java.io.IOException;

import androidx.annotation.NonNull;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ConfigUtil {
    public static final String TAG = "--ocean--";

    public static final String POST_BID = "http://mobile.jeanell.vip/data/dataAnalysis";//上传gaid，价格等地址
    public static final String POST_LOAD_LOG_URL = "http://192.168.40.190/top_selection/save_ad_load_log";

//        public static final String GET_CONFIG_URL = "http://mobile.jeanell.vip/exhibit/config";//获取配置地址
    public static final String GET_CONFIG_URL = "http://192.168.40.190/top_selection/config";
    public static final String POST_APP_START = "http://192.168.40.190/top_selection/save_app_start_log";
    public static final String POST_SHOW_LOG_URL = "http://192.168.40.190/top_selection/save_ad_show_log";

    public static final MediaType JSONMediaType = MediaType.parse("application/json; charset=utf-8");

    public static void initPostBid(JsonObject json) {
        Thread mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                postBid(json);
            }
        });
        mThread.start();
    }

    private static void postBid(JsonObject json) {
        Log.d("ocean", "json->" + json);
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(JSONMediaType, json.toString());
        Request request = new Request.Builder()
                .url(ConfigUtil.POST_BID)
                .post(requestBody)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            String requestJson = response.body().string();
            Log.d(TAG, "requestJson：" + requestJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void initPostLoadLog(JsonObject json) {
        Thread mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                postLoadLog(json);
            }
        });
        mThread.start();
    }

    private static void postLoadLog(JsonObject json) {
        Log.d("ocean", "postLoadLog json->" + json);
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(JSONMediaType, json.toString());
        Request request = new Request.Builder()
                .url(ConfigUtil.POST_LOAD_LOG_URL)
                .post(requestBody)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            String requestJson = response.body().string();
            Log.d(TAG, "postLoadLog requestJson：" + requestJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void initPostShowLog(JsonObject json) {
        Thread mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                postShowLog(json);
            }
        });
        mThread.start();
    }

    private static void postShowLog(JsonObject json) {
        Log.d("ocean", "show json->" + json);
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(JSONMediaType, json.toString());
        Request request = new Request.Builder()
                .url(ConfigUtil.POST_SHOW_LOG_URL)
                .post(requestBody)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            String requestJson = response.body().string();
            Log.d(TAG, "show requestJson：" + requestJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void getConfig(String pkg, final MyCallback callback) {
        String url = GET_CONFIG_URL + "?pkg=" + pkg;
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();

        // 发送异步GET请求
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                // 请求失败时回调onFailure方法
                callback.onFailure(e);
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                // 请求成功时回调onResponse方法
                if (response.isSuccessful()) {
                    final String responseData = response.body().string();
                    callback.onResponse(responseData);
                } else {
                    // 处理请求失败的情况
                    callback.onFailure(new IOException("Unexpected code " + response));
                }
                // 记得关闭响应，释放资源
                response.close();
            }
        });
    }

    // 定义一个接口用于回调请求结果
    public interface MyCallback {
        void onResponse(String result);

        void onFailure(IOException e);
    }

    public static void initPostAppStart(JsonObject json) {
        Thread mThread = new Thread(new Runnable() {
            @Override
            public void run() {
                postAppStart(json);
            }
        });
        mThread.start();
    }

    private static void postAppStart(JsonObject json) {
        Log.d("ocean", "POST_APP_START json->" + json);
        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = RequestBody.create(JSONMediaType, json.toString());
        Request request = new Request.Builder()
                .url(ConfigUtil.POST_APP_START)
                .post(requestBody)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            String requestJson = response.body().string();
            Log.d(TAG, "POST_APP_START requestJson：" + requestJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
