package com.rgt.superr.ui.batteryinfo

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

data class BatteryInfoBean(
    val icon: Int,
    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 rv = holder.itemView.findViewById<RecyclerView>(R.id.rv)
            rv.layoutManager = GridLayoutManager(holder.itemView.context, 2)
            rv.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))
        }
    }
}