package com.proyek.akhir.kodel.ui.koperasi.beranda.jatuhtempo

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.proyek.akhir.kodel.R
import com.proyek.akhir.kodel.data.response.UtangItem
import com.proyek.akhir.kodel.ui.user.dashboard.DashboardViewModel
import kotlinx.android.synthetic.main.activity_jatuh_tempo.*

class JatuhTempoActivity : AppCompatActivity() {
    private lateinit var jatuhTempoViewModel: DashboardViewModel

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

        rvPenunggakKoperasi.setHasFixedSize(true)
        rvPenunggakKoperasi.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

        refreshJatuhTempo.setOnRefreshListener {
            refreshJatuhTempo.isRefreshing = true
            showRecyclerPenunggak()
        }

        refreshJatuhTempo.isRefreshing = true
        showRecyclerPenunggak()
    }

    private fun showRecyclerPenunggak() {
        jatuhTempoViewModel = ViewModelProvider(this).get(DashboardViewModel::class.java)
        jatuhTempoViewModel.setDataJatuhTempo().observe(this, Observer { item ->
            if (item != null) {
                refreshJatuhTempo.isRefreshing = false
                showData(item)
            } else {
                refreshJatuhTempo.isRefreshing = false
                Toast.makeText(this, "Koneksi Lemah, Refresh!", Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun showData(item: List<UtangItem>){
        rvPenunggakKoperasi.adapter = JatuhTempoAdapter(item)
    }
}
