package kr.co.vividnext.sodalive.mypage.can.charge.pg

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.RecyclerView
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.databinding.ItemCanChargeBinding
import kr.co.vividnext.sodalive.extensions.fontSpan
import kr.co.vividnext.sodalive.extensions.moneyFormat

class CanChargePgAdapter(
    private val onClick: (CanResponse) -> Unit
) : RecyclerView.Adapter<CanChargePgAdapter.ViewHolder>() {

    val items = mutableListOf<CanResponse>()

    inner class ViewHolder(
        private val context: Context,
        private val binding: ItemCanChargeBinding
    ) : RecyclerView.ViewHolder(binding.root) {
        fun bind(canCharge: CanResponse) {
            binding.tvPrice.text = canCharge.price.moneyFormat()

            val typeface = ResourcesCompat.getFont(context, R.font.gmarket_sans_medium)
            binding.tvTitle.text = canCharge.title.fontSpan(
                typeface,
                "캔"
            )

            binding.root.setOnClickListener { onClick(canCharge) }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            parent.context,
            ItemCanChargeBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

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

    override fun getItemCount() = items.count()
}
