package com.nivesh.production.niveshfd.fd.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.nivesh.production.niveshfd.R
import com.nivesh.production.niveshfd.fd.model.GetCodes

class PaymentModeAdapter(
    private val listOfPayMode: MutableList<GetCodes>,

    private var selectedAmount: String? = null
) : RecyclerView.Adapter<PaymentModeAdapter.BankListViewHolder>() {
    inner class BankListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val paymentSelector: ImageView = itemView.findViewById(R.id.paymentSelector)
        val paymentMethod: TextView = itemView.findViewById(R.id.paymentMethod)
        val tvUpiMsg: TextView = itemView.findViewById(R.id.tvUpiMsg)
    }

    private var checkedPosition: Int = -2


    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BankListViewHolder {
        return BankListViewHolder(
            LayoutInflater.from(parent.context).inflate(
                R.layout.item_payment_list_preview,
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: BankListViewHolder, position: Int) {
        val listOfPayMode = listOfPayMode[position]
        holder.itemView.apply {

            holder.paymentMethod.text = listOfPayMode.Value
            if (listOfPayMode.Value == "UPI") {
                holder.tvUpiMsg.text = context.getString(R.string.upto1LakhOnly)
            } else {
                holder.tvUpiMsg.text = ""
            }



            if (selectedAmount != null && (checkedPosition == -2)) {
                holder.paymentSelector.setBackgroundResource(R.drawable.ic_select_green)
                checkedPosition = holder.adapterPosition
            } else if (checkedPosition == -1) {
                holder.paymentSelector.setBackgroundResource(R.drawable.ic_select_outline)
            } else if (checkedPosition == holder.adapterPosition) {
                holder.paymentSelector.setBackgroundResource(R.drawable.ic_select_green)
            } else {
                holder.paymentSelector.setBackgroundResource(R.drawable.ic_select_outline)
            }

            holder.itemView.setOnClickListener {
                holder.paymentSelector.setBackgroundResource(R.drawable.ic_select_green)
                if (checkedPosition != holder.adapterPosition) {
                    notifyItemChanged(checkedPosition)
                    checkedPosition = holder.adapterPosition
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return listOfPayMode.size
    }

    private var onItemClickListener: ((GetCodes) -> Unit)? = null

    fun setOnItemClickListener(listener: (GetCodes) -> Unit) {
        onItemClickListener = listener
    }


    fun getSelected(): GetCodes? {
        return if (checkedPosition != -1) {
            listOfPayMode[checkedPosition]
        } else null
    }

}