package com.gamedog.vididin.features.withdraw


import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.ama.core.architecture.util.ResUtil
import com.gamedog.vididin.manager.WithdrawSubItem
import com.viddin.videos.free.R
import com.viddin.videos.free.databinding.LayoutItemWithdrawSubBinding as ViewBinding

class WithdrawSubAdapter(private val selectCallback: (index: Int)->Unit) : ListAdapter<WithdrawSubItem, WithdrawSubAdapter.ViewHolder>(DiffCallback()) {

    var mSelectedSubIndex: Int = 0

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.itemView.rootView.setOnClickListener {
            selectCallback.invoke(position)
        }
        holder.bind(getItem(position))
    }


    inner class ViewHolder(private val binding: ViewBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(item: WithdrawSubItem) {
            with(binding) {
                tvTitle.text = buildString {
                    append(ResUtil.getString(R.string.cash))
                    append(item.cashTotal)
                }
                tvDay.text = buildString {
                    append(ResUtil.getString(R.string.day))
                    append(" ")
                    append(item.dayIndex + 1)
                }

                if (item.dayIndex == mSelectedSubIndex) {
                    root.setBackgroundResource(R.drawable.bg_withdraw_sub_selected)
                    root.alpha = 1F
                    ivLefttopChecked.isVisible = true
                } else {
                    root.setBackgroundResource(R.drawable.bg_withdraw_sub_unselected)
                    root.alpha = 0.7F
                    ivLefttopChecked.isVisible = false
                }
            }
        }
    }

    class DiffCallback : DiffUtil.ItemCallback<WithdrawSubItem>() {
        override fun areItemsTheSame(oldItem: WithdrawSubItem, newItem: WithdrawSubItem): Boolean {
            return oldItem.dayIndex == newItem.dayIndex
        }

        override fun areContentsTheSame(oldItem: WithdrawSubItem, newItem: WithdrawSubItem): Boolean {
            return newItem == oldItem
        }
    }
}