package com.example.uitugasakhir.api

import android.content.Context
import android.util.Log
import com.android.volley.Request
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.example.uitugasakhir.model.AmbangBatas
import org.json.JSONObject

object ApiAmbangBatas {

    // 🔥 GET DATA
    fun getAmbangBatas(
        context: Context,
        onSuccess: (AmbangBatas) -> Unit,
        onError: (String) -> Unit
    ) {

        val url =
            "http://kopikaosmartsystem.my.id/get_ambang_batas.php"

        val request = StringRequest(
            Request.Method.GET,
            url,
            { response ->

                try {
                    Log.d("API_GET_RAW", response)

                    val json = JSONObject(response)

                    if (json.getString("status") == "success") {

                        val data = AmbangBatas(
                            min_tanah = json.getInt("min_tanah"),
                            min_suhu = json.getInt("min_suhu"),
                            min_udara = json.getInt("min_udara"),
                            max_tanah = json.getInt("max_tanah"),
                            max_suhu = json.getInt("max_suhu"),
                            max_udara = json.getInt("max_udara"),
                            critical_tanah = json.getInt("critical_tanah")
                        )

                        onSuccess(data)

                    } else {
                        onError("Data tidak tersedia")
                    }

                } catch (e: Exception) {
                    Log.e("JSON_ERROR_GET", response)
                    onError("Response bukan JSON (cek server/API error)")
                }
            },
            { error ->

                Log.e("GET_ERROR", error.toString())

                error.networkResponse?.let {

                    Log.e(
                        "GET_CODE",
                        it.statusCode.toString()
                    )

                    Log.e(
                        "GET_BODY",
                        String(it.data)
                    )
                }

                onError(error.toString())
            }
        )

        Volley.newRequestQueue(context).add(request)
    }


    // 🔥 SAVE / UPDATE DATA
    fun saveAmbangBatas(
        context: Context,
        minTanah: Int,
        minSuhu: Int,
        minUdara: Int,
        maxTanah: Int,
        maxSuhu: Int,
        maxUdara: Int,
        criticalTanah: Int,
        onSuccess: (String) -> Unit,
        onError: (String) -> Unit
    ) {

        val url = "http://kopikaosmartsystem.my.id/save_ambang_batas.php"

        val request = object : StringRequest(
            Method.POST,
            url,
            { response ->

                try {
                    Log.d("API_SAVE_RAW", response)

                    val json = JSONObject(response)
                    onSuccess(json.getString("message"))

                } catch (e: Exception) {
                    Log.e("JSON_ERROR_SAVE", response)
                    onError("Response bukan JSON (cek server/API error)")
                }
            },
            { error ->

                Log.e("SAVE_ERROR", error.toString())

                error.networkResponse?.let {

                    Log.e(
                        "SAVE_CODE",
                        it.statusCode.toString()
                    )

                    Log.e(
                        "SAVE_BODY",
                        String(it.data)
                    )
                }

                onError(error.toString())
            }
        ) {

            override fun getParams(): MutableMap<String, String> {
                return hashMapOf(
                    "min_tanah" to minTanah.toString(),
                    "min_suhu" to minSuhu.toString(),
                    "min_udara" to minUdara.toString(),
                    "max_tanah" to maxTanah.toString(),
                    "max_suhu" to maxSuhu.toString(),
                    "max_udara" to maxUdara.toString(),
                    "critical_tanah" to criticalTanah.toString()
                )
            }
        }

        Volley.newRequestQueue(context).add(request)
    }
}