package com.cake.draw.painting.environment.hy

import android.util.Log
import com.google.gson.JsonObject
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException

object MyConfigUtil {
    const val TAG = "ocean-lux-api"
    private const val POST_LOAD_LOG_URL = "http://172.24.100.10:8278/ad_report/save_ad_load_log"
    private const val GET_CONFIG_URL = "http://172.24.100.10:8278/top_selection/config"
    private const val POST_SHOW_LOG_URL = "http://172.24.100.10:8278/ad_report/save_ad_show_log"

    private val httpUtil = HttpUtil.mInstance

    fun initPostLoadLog(json: JsonObject) {
        httpUtil.post(POST_LOAD_LOG_URL, json.toString(), object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                Log.d(TAG, "initPostLoadLog onFailure->$e")
            }

            override fun onResponse(call: Call, response: Response) {
                Log.d(TAG, "initPostLoadLog onResponse->$response")
            }
        })
    }

    fun initPostShowLog(json: JsonObject) {
        httpUtil.post(POST_SHOW_LOG_URL, json.toString(), object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                Log.d(TAG, "initPostShowLog onFailure->$e")
            }

            override fun onResponse(call: Call, response: Response) {
                Log.d(TAG, "initPostShowLog onResponse->$response")
            }
        })
    }

    fun getConfig(pkg: String, callback: ConfigCallback) {
        val url = "$GET_CONFIG_URL?pkg=$pkg"
        Log.d(TAG,"config url ->$url")
        httpUtil.get(url, object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                Log.d(TAG, "getConfig onFailure")
                Log.d(TAG, "exception=${e.javaClass.simpleName}")
                Log.d(TAG, "message=${e.message}")
                Log.d(TAG, "stacktrace=${Log.getStackTraceString(e)}")

                callback.onFailure(e)
            }

            override fun onResponse(call: Call, response: Response) {
                try {
                    if (response.isSuccessful) {
                        val responseData = response.body?.string()
                        if (responseData == null) {
                            Log.d(TAG, "response body is null")
                            callback.onFailure(IOException("response body is null"))
                            return
                        }
                        callback.onResponse(responseData)
                    } else {
                        val errorBody = response.body?.string()

                        Log.d(TAG, "getConfig http fail")
                        Log.d(TAG, "code=${response.code}")
                        Log.d(TAG, "message=${response.message}")
                        Log.d(TAG, "body=$errorBody")

                        callback.onFailure(
                            IOException("HTTP ${response.code}, body=$errorBody")
                        )
                    }
                } finally {
                    response.close()
                }
            }
        })
    }
}