package com.example.projekta

import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley

class PanelSuryaActivity : AppCompatActivity() {

    private lateinit var tvStatusPanel: TextView
    private lateinit var tvDaya: TextView
    private lateinit var tvStatusDaya: TextView
    private lateinit var tvSumberEnergi: TextView
    private lateinit var tvStatusSistem: TextView
    private lateinit var tvStatusPompa: TextView
    private lateinit var tvUpdate: TextView
    private lateinit var progressDaya: ProgressBar
    private lateinit var btnRefresh: Button

    private val url =
        "http://192.168.43.191/projekta/get_panel_surya.php"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_panel_surya)

        findViewById<ImageView>(R.id.btnBack).setOnClickListener {
            finish()
        }

        tvStatusPanel = findViewById(R.id.tvStatusPanel)
        tvDaya = findViewById(R.id.tvDaya)
        tvStatusDaya = findViewById(R.id.tvStatusDaya)
        tvSumberEnergi = findViewById(R.id.tvSumberEnergi)
        tvStatusSistem = findViewById(R.id.tvStatusSistem)
        tvStatusPompa = findViewById(R.id.tvStatusPompa)
        tvUpdate = findViewById(R.id.tvUpdate)
        progressDaya = findViewById(R.id.progressDaya)
        btnRefresh = findViewById(R.id.btnRefresh)

        ambilDataPanelSurya()

        btnRefresh.setOnClickListener {
            ambilDataPanelSurya()
        }
    }

    private fun ambilDataPanelSurya() {

        val request = JsonObjectRequest(
            Request.Method.GET,
            url,
            null,

            { response ->

                val statusPanel =
                    response.optString("status_panel", "-")

                val daya =
                    response.optInt("daya", 0)

                val statusDaya =
                    response.optString("status_daya", "-")

                val sumberEnergi =
                    response.optString("sumber_energi", "-")

                val statusSistem =
                    response.optString("status_sistem", "-")

                val statusPompa =
                    response.optString("status_pompa", "-")

                val waktuUpdate =
                    response.optString("waktu_update", "-")

                tvStatusPanel.text = "🟢 $statusPanel"

                tvDaya.text = "$daya%"

                progressDaya.progress = daya

                when {
                    daya >= 80 -> {
                        tvStatusDaya.text = "🟢 $statusDaya"
                    }

                    daya >= 40 -> {
                        tvStatusDaya.text = "🟡 $statusDaya"
                    }

                    else -> {
                        tvStatusDaya.text = "🔴 $statusDaya"
                    }
                }

                tvSumberEnergi.text =
                    "🔆 Sumber Energi : $sumberEnergi"

                tvStatusSistem.text =
                    "⚙️ Status Sistem : $statusSistem"

                tvStatusPompa.text =
                    "🚰 Status Pompa : $statusPompa"

                tvUpdate.text =
                    "🕒 Update Terakhir : $waktuUpdate"

            },

            { error ->

                tvStatusPanel.text = "❌ Gagal mengambil data"

                tvDaya.text = "0%"

                progressDaya.progress = 0

                tvStatusDaya.text = "-"

                tvSumberEnergi.text =
                    "🔆 Sumber Energi : -"

                tvStatusSistem.text =
                    "⚙️ Status Sistem : -"

                tvStatusPompa.text =
                    "🚰 Status Pompa : -"

                tvUpdate.text =
                    "🕒 Update Terakhir : -"

                error.printStackTrace()
            }
        )

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