package com.base.browserwhite.ui.activity.search

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.base.browserwhite.R
import com.base.browserwhite.bean.SearchRecordBean
import com.base.browserwhite.databinding.ItemSearchrecordBinding
import com.base.browserwhite.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter

class SearchRecordAdapter : BaseQuickAdapter<SearchRecordBean, SearchRecordAdapter.SearchRecordViewHolder>() {
    inner class SearchRecordViewHolder(view: View) : ViewHolder(view) {

    }

    var jumpAction: ((content: String) -> Unit)? = null
    var deleteAction: (() -> Unit)? = null

    override fun onBindViewHolder(holder: SearchRecordViewHolder, position: Int, item: SearchRecordBean?) {
        if (item == null) return

        val binding = ItemSearchrecordBinding.bind(holder.itemView)
        binding.ivDelete.isVisible = item.isDelete
        binding.tvContent.isVisible = !item.isDelete
        binding.tvContent.text = item.content

        binding.tvContent.setOnClickListener {
            jumpAction?.invoke(item.content)
        }
        binding.ivDelete.setOnClickListener {
            deleteAction?.invoke()
        }

    }

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