package com.base.scanqrclear.qr

import android.annotation.SuppressLint
import android.content.Context
import android.view.ViewGroup
import androidx.core.view.isVisible
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.QrStyleBean
import com.base.scanqrclear.databinding.ItemQrStyleBinding
import com.base.scanqrclear.ui.adapter.CommonViewHolder
import com.base.scanqrclear.utils.LogEx
import com.base.scanqrclear.utils.XmlEx.inflate
import com.bumptech.glide.Glide
import com.chad.library.adapter4.BaseQuickAdapter

class QrStyleAdapter : BaseQuickAdapter<QrStyleBean, CommonViewHolder>() {

    private val TAG = "QrStyleAdapter"

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: QrStyleBean?) {
        item ?: return
        val binding = ItemQrStyleBinding.bind(holder.itemView)
        val context = holder.itemView.context
        LogEx.logDebug(TAG, item.preview_url)
        Glide.with(context).load(item.preview_url).into(binding.iv)
        binding.ivSelect.isVisible = item.isSelect

        binding.root.setOnClickListener {
            if (item.isSelect) return@setOnClickListener
            changeSelectItem(item)
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun changeSelectItem(item: QrStyleBean) {
        items.filter { it.isSelect }.map { it.isSelect = false }
        item.isSelect = true
        notifyDataSetChanged()
    }

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

}