package com.quyunshuo.module.home.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PayEnum
import com.quyunshuo.androidbaseframemvvm.common.bean.PayTypeBean
import com.quyunshuo.androidbaseframemvvm.common.listener.AdapterClickListener
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import com.quyunshuo.module.home.databinding.HomeItemPayChooseBinding

class PayChooseDialogAdapter(var productList:HashMap<String, PayTypeBean>): RecyclerView.Adapter<PayChooseDialogAdapter.MyViewHolder>() {
    private val TAG = "HomeProductAdapter"
    private val _position = MutableLiveData<Int>()
    val clickPosition:LiveData<Int> =  _position
    var keyList = ArrayList<String>()
    var adapter:AdapterClickListener? = null
    init{
        for ((index, s) in productList.keys.withIndex()) {
            keyList.add(s)
        }
    }

    inner class MyViewHolder(binding: HomeItemPayChooseBinding): RecyclerView.ViewHolder(binding.root) {
        private val mBinding = binding
        fun bind(payTypeBean: PayTypeBean) {
            mBinding.run {
                val valueOf = PayEnum.getEnumByValue(payTypeBean.payCode)
                tvName.text = UiUtil.getStringRes(valueOf.nameId)
                ivSugar.setImageResource(valueOf.imgId)
                ivSugar.setOnClickListener {
                    Log.d(TAG, "layoutPosition ivSugar: $layoutPosition")
                    adapter?.onClickListener(it,layoutPosition,payTypeBean.payCode)
                }
                clPay.setOnClickListener{
                    Log.d(TAG, "layoutPosition clPay: $layoutPosition")

                }
            }
        }




    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val projectVH = MyViewHolder(
            HomeItemPayChooseBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        )
        return projectVH
    }

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

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        var key = keyList.get(position)
        val payTypeBean:PayTypeBean =productList.get(key)!!
        holder.bind(payTypeBean)

    }
    fun setOnclickListener(adapter:AdapterClickListener){
        this.adapter = adapter
    }
}