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.BackstageItemPermissionBinding
import com.module.backstage.enums.SettingItemEnum
import com.quyunshuo.androidbaseframemvvm.base.utils.SpUtils
import com.quyunshuo.androidbaseframemvvm.common.bean.PayTypeBean
import com.quyunshuo.androidbaseframemvvm.common.bean.SettingTypeBean
import com.quyunshuo.androidbaseframemvvm.common.constant.MMKVName
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil

class PermissionAdapter(var productList: MutableList<SettingItemEnum>) :
    RecyclerView.Adapter<PermissionAdapter.MyViewHolder>() {
    private val TAG = "HomeProductAdapter"
    private val _position = MutableLiveData<Int>()
    val clickPosition: LiveData<Int> = _position
//    var arrayListState = ArrayList<PayTypeBean>()
    var arrayListState: HashMap<Int, SettingTypeBean> = HashMap()
    init {
        val productStr = SpUtils.getString(MMKVName.SETTING_TYPE, "")
        if (productStr=="") {
            productList.forEach {
                arrayListState[it.id] = SettingTypeBean(it.id,it.chName,true)
                Log.d(TAG, "productStr : "+it.id)
            }
            SpUtils.putString(MMKVName.SETTING_TYPE,Gson().toJson(arrayListState))
        }else{
            arrayListState = Gson().fromJson(
                productStr,
                object : TypeToken<HashMap<Int, SettingTypeBean>?>() {}.type
            )
        }


    }

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

        fun bind(settingItemEnum: SettingItemEnum, position: Int) {
            mBinding.run {
                cbPay.text = UiUtil.getStringRes(settingItemEnum.nameId)
                ivIcon.setImageResource(settingItemEnum.iconId)
                val payTypeBean = arrayListState[settingItemEnum.id]

                if (payTypeBean!=null) {
                    cbPay.isChecked = payTypeBean.isSelected
                }
//                Log.d(TAG, "payTypeBean bind: $payTypeBean")
                cbPay.setOnCheckedChangeListener { buttonView, isChecked ->
//                    if (isChecked) {
                        Log.d(TAG, "bind: $settingItemEnum"+settingItemEnum.id  )
                        //如果为true 就增加。
                        arrayListState[settingItemEnum.id]?.isSelected = isChecked /*= SettingTypeBean(settingItemEnum.id,settingItemEnum.chName,isChecked)*/
//                    }else{
                        //如果为false,就删除
//                        arrayListState[settingItemEnum.id]?.isSelected = isChecked /*= SettingTypeBean(settingItemEnum.id,settingItemEnum.chName,isChecked)*/
//                    }
                }
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val projectVH = MyViewHolder(
            BackstageItemPermissionBinding.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)
    }
}