package com.proyek.akhir.kodel.ui.koperasi.berita

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.proyek.akhir.kodel.R
import com.proyek.akhir.kodel.data.response.BeritaItem
import kotlinx.android.synthetic.main.item_berita.view.*
import java.text.SimpleDateFormat
import java.util.*

class BeritaKoperasiAdapter(private val listBerita: List<BeritaItem>) :
    RecyclerView.Adapter<BeritaKoperasiAdapter.BeritaKoperasiViewHolder>() {

    inner class BeritaKoperasiViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(berita: BeritaItem) {
            with(itemView) {
                judulBerita.text = berita.judulBerita
                val inputFormatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.US)
                val outputFormatter = SimpleDateFormat("dd-MM-yyyy", Locale.US)
                val answer : Date = inputFormatter.parse(berita.tglBerita)
                val tanggal  = outputFormatter.format(answer)
                tglBerita.text = tanggal.toString()
            }
        }

    }

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

    override fun getItemCount(): Int = listBerita.size

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