package com.example.projekta

import android.os.Bundle
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.android.volley.Request
import com.android.volley.toolbox.JsonArrayRequest
import com.android.volley.toolbox.Volley
import org.json.JSONObject

class HistoriActivity : AppCompatActivity() {

    private lateinit var recyclerHistori: RecyclerView

    private lateinit var historiList:
            ArrayList<HistoriModel>

    private lateinit var historiAdapter:
            HistoriAdapter

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_histori)

        // ================= BACK =================
        val btnBack =
            findViewById<ImageView>(R.id.btnBack)

        btnBack.setOnClickListener {
            finish()
        }

        // ================= RECYCLERVIEW =================
        recyclerHistori =
            findViewById(R.id.recyclerHistori)

        recyclerHistori.layoutManager =
            LinearLayoutManager(this)

        historiList = ArrayList()

        historiAdapter =
            HistoriAdapter(
                this,
                historiList
            )

        recyclerHistori.adapter =
            historiAdapter

        // ================= LOAD HISTORI =================
        loadHistori()
    }

    private fun loadHistori() {

        val url =
            "http://192.168.43.191/projekta/get_histori.php"

        val request = JsonArrayRequest(

            Request.Method.GET,
            url,
            null,

            { response ->

                historiList.clear()

                for (i in 0 until response.length()) {

                    val data: JSONObject =
                        response.getJSONObject(i)

                    val histori = HistoriModel(

                        createdAt =
                            data.getString("created_at"),

                        jamPemupukan =
                            data.getString("jam_pemupukan"),

                        mode =
                            data.getString("mode"),

                        durasiPompa =
                            data.getString("durasi_pompa"),

                        intervalHari =
                            data.getString("interval_hari"),

                        statusTanah =
                            data.getString("status_tanah")
                    )

                    historiList.add(histori)
                }

                historiAdapter.notifyDataSetChanged()
            },

            {

                Toast.makeText(
                    this,
                    "Gagal mengambil histori",
                    Toast.LENGTH_SHORT
                ).show()
            }
        )

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