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

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
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.TarikSaldoItem
import com.proyek.akhir.kodel.ui.koperasi.beranda.HomeViewModel
import kotlinx.android.synthetic.main.activity_tarik_saldo2.*

class TarikSaldoActivity : AppCompatActivity() {
    private lateinit var tarikSaldoViewModel: HomeViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tarik_saldo2)
        rvTarikSaldo.setHasFixedSize(true)
        rvTarikSaldo.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)

        refreshTarikSaldo.setOnRefreshListener {
            refreshTarikSaldo.isRefreshing = true
            showAllData()
        }

        refreshTarikSaldo.isRefreshing = true
        showAllData()
    }

    private fun showAllData() {
        tarikSaldoViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
        tarikSaldoViewModel.getTarikSaldo()
        tarikSaldoViewModel.setDataTarikSaldo().observe(this, Observer { item ->
            if (item != null) {
                refreshTarikSaldo.isRefreshing = false
                showData(item)
            } else {
                refreshTarikSaldo.isRefreshing = false
                Toast.makeText(this, "Koneksi Lemah, Refresh!", Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun showData(item: List<TarikSaldoItem>) {
        rvTarikSaldo.adapter = TarikSaldoAdapter(item)

    }

}
