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

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.local.History
import com.proyek.akhir.kodel.data.response.TransaksiItem
import com.proyek.akhir.kodel.ui.user.dashboard.history.HistoryViewModel
import kotlinx.android.synthetic.main.activity_history_koperasi.*

class HistoryKoperasiActivity : AppCompatActivity() {
    private lateinit var historyViewModel: HistoryViewModel

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

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

        refreshHistoryKoperasi.setOnRefreshListener {
            refreshHistoryKoperasi.isRefreshing = true
            showRecyclerView()
        }

        refreshHistoryKoperasi.isRefreshing = tr=ue
        showRecyclerView()
    }

    private fun showRecyclerView() {
        historyViewModel = ViewModelProvider(this).get(HistoryViewModel::class.java)
        historyViewModel.setDataTransaksi().observe(this, Observer { transaksi ->
            if(transaksi != null){
                showDataTransaksi(transaksi)
                refreshHistoryKoperasi.isRefreshing = false
            }else{
                refreshHistoryKoperasi.isRefreshing = false
                Toast.makeText(this, "Koneksi Lemah, Refresh!", Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun showDataTransaksi(transaksi: List<TransaksiItem>){
        rvHistoryKoperasi.adapter = HistoryKoperasiAdapter(transaksi)
    }
}
