package com.easy.clean.ui.batteryinfo

import android.content.Context
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.easy.clean.R
import com.easy.clean.databinding.ItemBatteryInfoBinding
import com.easy.clean.ui.adapter.CommonViewHolder
import com.easy.clean.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter

data class BatteryInfoBean(
    var tvValue: String,
    val title: String,
)

class BatteryInfoAdapter : BaseQuickAdapter<List<BatteryInfoBean>, CommonViewHolder>() {

    var initSub: Boolean = false
    val subAdapter = SubAdapter()

    override fun onBindViewHolder(
        holder: CommonViewHolder,
        position: Int,
        item: List<BatteryInfoBean>?
    ) {
        item ?: return
        if (!initSub) {
            initSub = true
            val recyclerView = holder.itemView.findViewById<RecyclerView>(R.id.rv)
            recyclerView.layoutManager = GridLayoutManager(holder.itemView.context, 2)
            recyclerView.adapter = subAdapter
        }
        subAdapter.submitList(item)
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): CommonViewHolder {
        return CommonViewHolder(R.layout.item_rv.inflate(parent))
    }


    inner class SubAdapter : BaseQuickAdapter<BatteryInfoBean, CommonViewHolder>() {
        override fun onBindViewHolder(
            holder: CommonViewHolder,
            position: Int,
            item: BatteryInfoBean?
        ) {
            item ?: return

            val binding = ItemBatteryInfoBinding.bind(holder.itemView)
//            binding.iv.setImageResource(item.icon)
            binding.tvValue.text = item.tvValue
            binding.tvTitle.text = item.title

        }

        override fun onCreateViewHolder(
            context: Context,
            parent: ViewGroup,
            viewType: Int
        ): CommonViewHolder {
            return CommonViewHolder(R.layout.item_battery_info.inflate(parent))
        }
    }
}