package com.proyek.akhir.kodel.ui.user.dashboard.history


import android.os.Bundle
import android.view.View
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.local.History
import com.proyek.akhir.kodel.data.response.TransaksiItem
import kotlinx.android.synthetic.main.activity_history.*


class HistoryActivity : AppCompatActivity() {
    private lateinit var viewModelTransaksi: HistoryViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_history)
        rvHistory.setHasFixedSize(true)
        rvHistory.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        progressBarHistory.visibility = View.VISIBLE
        swipeBarHistory.setOnRefreshListener {
            swipeBarHistory.isRefreshing = true
            showRecyclerView()
            swipeBarHistory.isRefreshing = false
        }
        showRecyclerView()
    }

    private fun showRecyclerView() {
        viewModelTransaksi = ViewModelProvider(this).get(HistoryViewModel::class.java)
        viewModelTransaksi.setDataTransaksi().observe(this, Observer { transaksi ->
            if (transaksi != null) {
                progressBarHistory.visibility = View.INVISIBLE
                showData(transaksi)
            } else {
                Toast.makeText(this, "Jaringan Lemah Silahkan Refresh", Toast.LENGTH_SHORT).show()
            }
        })
    }

    private fun showData(item: List<TransaksiItem>) {
        rvHistory.adapter = HistoryAdapter(item)
    }
}
