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

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.proyek.akhir.kodel.R
import com.proyek.akhir.kodel.data.response.IsiSaldoItem
import com.proyek.akhir.kodel.data.response.IsiSaldos
import kotlinx.android.synthetic.main.item_isi_saldo.view.*

class IsiSaldoAdapter(private val listIsiSaldo: List<IsiSaldoItem>) :
    RecyclerView.Adapter<IsiSaldoAdapter.IsiSaldoViewHolder>() {

    inner class IsiSaldoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(isiSaldo: IsiSaldoItem) {
            with(itemView) {
                Glide.with(itemView.context)
                    .load(isiSaldo.imgAnggota)
                    .into(imgPenggunaIsiSaldo)
                namePenggunaIsiSaldo.text = isiSaldo.namaAnggota
                jlhIsiSaldo.text = isiSaldo.jlhSaldo.toString()
                confirmIsiSaldo.setOnClickListener {
                    val isiSaldo = IsiSaldos(
                        1,
                        isiSaldo.idAnggota,
                        isiSaldo.imgAnggota,
                        isiSaldo.jlhSaldo,
                        isiSaldo.namaAnggota,
                        true
                    )

                    (context as IsiSaldoActivity).postIsiSaldo(isiSaldo)
                }
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IsiSaldoViewHolder =
        IsiSaldoViewHolder(
            LayoutInflater.from(parent.context).inflate(
                R.layout.item_isi_saldo, parent, false
            )
        )

    override fun getItemCount(): Int = listIsiSaldo.size

    override fun onBindViewHolder(holder: IsiSaldoViewHolder, position: Int) {
        holder.bind(listIsiSaldo[position])
    }
}