package com.nivesh.production.partnerOnBoarding.adapters

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.textfield.TextInputLayout
import com.nivesh.production.partnerOnBoarding.R
import com.nivesh.production.partnerOnBoarding.model.response.DataX
import com.nivesh.production.partnerOnBoarding.ui.fragments.NewDistributorSignStepOne

class NewDistributorSignStepOneAdapter(private var listData: List<DataX>) :
    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].ProductName
        holder.checkBox.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                if (listData[position].ProductName == "Insurance") holder.rlInsurance.visibility = View.VISIBLE
                else holder.rlInsurance.visibility = View.GONE
                if (listData[position].ProductName == "Other") holder.tlOtherName.visibility = View.VISIBLE
                else holder.tlOtherName.visibility = View.GONE

                NewDistributorSignStepOne.listMapData = mutableMapOf(position to listData[position].ProductName)
            } else {
                if (listData[position].ProductName == "Insurance")
                    holder.rlInsurance.visibility = View.GONE
                if (listData[position].ProductName == "Other")
                    holder.tlOtherName.visibility = View.GONE

                NewDistributorSignStepOne.listMapData?.remove(position)
            }
        }

        holder.tvPosInsurer.setOnClickListener {
            if (isPosInsurer == true) {
                holder.tvPosInsurer.setBackgroundResource(R.drawable.round_corner_grey_button)
                holder.tvPosInsurer.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
                holder.tvPosInsurer.compoundDrawablePadding = 0
                isPosInsurer = false
            } else {
                holder.tvPosInsurer.setBackgroundResource(R.drawable.round_corner_grey_button_fill)
                holder.tvPosInsurer.setCompoundDrawablesWithIntrinsicBounds(
                    0,
                    0,
                    R.drawable.svg_tik,
                    0
                )
                holder.tvPosInsurer.compoundDrawablePadding = 20
                isPosInsurer = true
            }
        }

        holder.tvPOSBroker.setOnClickListener {
            if (isPosBroker == true) {
                holder.tvPOSBroker.setBackgroundResource(R.drawable.round_corner_grey_button)
                holder.tvPOSBroker.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
                holder.tvPOSBroker.compoundDrawablePadding = 0
                isPosBroker = false
            } else {
                holder.tvPOSBroker.setBackgroundResource(R.drawable.round_corner_grey_button_fill)
                holder.tvPOSBroker.setCompoundDrawablesWithIntrinsicBounds(
                    0,
                    0,
                    R.drawable.svg_tik,
                    0
                )
                holder.tvPOSBroker.compoundDrawablePadding = 20
                isPosBroker = true
            }
        }

        holder.radioGroup.setOnCheckedChangeListener { group, checkedId ->
            val rb: RadioButton = group.findViewById(checkedId)
            if(rb.text.equals("Yes"))
                holder.llPOS.visibility = View.VISIBLE
            else holder.llPOS.visibility = View.GONE
        }

    }

    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)
        val tlOtherName: TextInputLayout = itemView.findViewById(R.id.tlOtherName)
        val radioGroup: RadioGroup = itemView.findViewById(R.id.radioGroup)
        val llPOS: LinearLayout = itemView.findViewById(R.id.llPOS)
    }
}