package com.module.backstage.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.module.backstage.databinding.BackstageItemPayBinding
import com.quyunshuo.androidbaseframemvvm.base.addressenum.PayEnum
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.common.bean.PayTypeBean
import com.quyunshuo.androidbaseframemvvm.common.constant.Heartbeat
import com.quyunshuo.androidbaseframemvvm.common.constant.MMKVName
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil

class PayAdapter(var productList: MutableList<PayEnum>) :
    RecyclerView.Adapter<PayAdapter.MyViewHolder>() {
    private val TAG = "HomeProductAdapter"
    private val _position = MutableLiveData<Int>()
    val clickPosition: LiveData<Int> = _position
//    var arrayListState = ArrayList<PayTypeBean>()
    var arrayListState: HashMap<String, PayTypeBean> = HashMap()
    init {

        val productStr = SpUtils.getString(MMKVName.PAY_TYPE, "")
        if (productStr!=""){
            arrayListState = Gson().fromJson(
                productStr,
                object : TypeToken<HashMap<String, PayTypeBean>?>() {}.type
            )
        }else{
            if (Heartbeat.isForeign) {
                arrayListState[PayEnum.BILL_COIN.code] = PayTypeBean(PayEnum.BILL_COIN.code,true)
            }else{
                arrayListState[PayEnum.TWO_CODE.code] = PayTypeBean(PayEnum.TWO_CODE.code,true)
            }
        }


    }

    inner class MyViewHolder(binding: BackstageItemPayBinding) :
        RecyclerView.ViewHolder(binding.root) {
        private val mBinding = binding

        fun bind(payEnum: PayEnum, position: Int) {
            mBinding.run {
                cbPay.text = UiUtil.getStringRes(payEnum.nameId)
                val payTypeBean = arrayListState[payEnum.code]
                if (payTypeBean!=null) {
                    cbPay.isChecked = payTypeBean.isSelected
                }
//                Log.d(TAG, "payTypeBean bind: $payTypeBean")
                cbPay.setOnCheckedChangeListener { buttonView, isChecked ->
                    if (isChecked) {
                        Log.d(TAG, "bind: $payEnum")
                        //如果为true 就增加。
                        arrayListState[payEnum.code] = PayTypeBean(payEnum.code,isChecked)
                    }else{
                        //如果为false,就删除
                        arrayListState.remove(payEnum.code)
                    }
                }
            }
        }
    }

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

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

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.bind(productList.get(position),position)
    }
}