package com.nivesh.production.niveshfd.partnerOnBoarding.adapters1

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.nivesh.production.niveshfd.R

class NewDistributorSignStepOneAdapter(private var listData: MutableList<String>) :
    RecyclerView.Adapter<NewDistributorSignStepOneAdapter.ViewHolder>() {
    private var context: Context? = null
    private var isPosBroker: Boolean? = false
    private var isPosInsurer: Boolean? = false

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.checkBox.text = listData[position]
        holder.checkBox.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                if (listData[position] == "Insurance") holder.rlInsurance.visibility = View.VISIBLE
                else holder.rlInsurance.visibility = View.GONE
            } else {
                holder.rlInsurance.visibility = View.GONE
            }
        }

        holder.tvPosInsurer.setOnClickListener {
//            isPosInsurer?.let { it1 -> setSelected(holder.tvPosInsurer, it1, 0) }
        }

        holder.tvPOSBroker.setOnClickListener {
//            isPosBroker?.let { it1 -> setSelected(holder.tvPOSBroker, it1, 1) }
            setSelected(holder.tvPOSBroker,isPosBroker,1)
        }
    }

    private fun setSelected(textView: TextView, check: Boolean?, pos: Int) {
        if (check == true) {
            textView.setCompoundDrawablesWithIntrinsicBounds(
                0,
                0,
                0,
                0
            )
            textView.compoundDrawablePadding = 0
            if(pos==0) isPosBroker = false
            else isPosInsurer = false
        } else {
            textView.setCompoundDrawablesWithIntrinsicBounds(
                0,
                0,
                R.drawable.svg_tik,
                0
            )
            textView.compoundDrawablePadding = 20
            if(pos==0) isPosBroker = true
            else isPosInsurer = true
        }
    }

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

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val checkBox: CheckBox = itemView.findViewById(R.id.checkBox)
        val rlInsurance: RelativeLayout = itemView.findViewById(R.id.rlInsurance)
        val tvPosInsurer: TextView = itemView.findViewById(R.id.tvPosInsurer)
        val tvPOSBroker: TextView = itemView.findViewById(R.id.tvPOSBroker)
    }
}