package com.adealink.weparty.profile.search.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.base.fastLazy
import com.adealink.frame.statistics.BaseStatEvent
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.recycleview.adapter.BindingViewHolder
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.adapter.multitype.ItemViewBinder
import com.adealink.weparty.commonui.recycleview.diffutil.BaseListItemData
import com.adealink.weparty.commonui.recycleview.itemdecoration.GridSpacingItemDecoration
import com.adealink.weparty.commonui.recycleview.itemdecoration.VerticalSpaceItemDecoration
import com.adealink.weparty.media.MediaConfig.context
import com.adealink.weparty.module.playmate.event.PlaymateListEventReporter
import com.adealink.weparty.module.playmate.event.PlaymateStatEvent
import com.adealink.weparty.profile.databinding.LayoutSearchAllRoomBinding
import com.adealink.weparty.profile.databinding.LayoutSearchAllUserBinding
import com.adealink.weparty.profile.search.data.SearchAllRoomResultItemData
import com.adealink.weparty.profile.search.data.SearchAllUserResultItemData
import com.adealink.weparty.profile.search.data.SearchUserResultItemData
import com.adealink.weparty.R as APP_R

class SearchAllRoomItemViewBinder(
    private val listener: SearchRoomItemViewBinder.OnRoomResultClickListener,
    private val clickMore: () -> Unit
) :
    ItemViewBinder<SearchAllRoomResultItemData, SearchAllRoomItemViewBinder.ViewHolder>() {

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: ViewHolder,
        item: SearchAllRoomResultItemData,
    ) {
        holder.update(item)
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): ViewHolder {
        return ViewHolder(LayoutSearchAllRoomBinding.inflate(inflater, parent, false)).also {
            it.initView()
        }
    }

    inner class ViewHolder(
        binding: LayoutSearchAllRoomBinding,
    ) : BindingViewHolder<LayoutSearchAllRoomBinding>(binding) {

        private val resultAdapter by fastLazy { MultiTypeListAdapter<BaseListItemData>() }

        private var item: SearchAllRoomResultItemData? = null
        fun initView() {
            resultAdapter.register(SearchRoomItemViewBinder(listener))
            binding.rvResult.layoutManager = GridLayoutManager(context, 2)
            binding.rvResult.adapter = resultAdapter
            binding.rvResult.addItemDecoration(
                GridSpacingItemDecoration(2, 13.dp(), 16.dp(), false)
            )
            binding.btnMore.onClick {
                clickMore.invoke()
            }
        }

        fun update(item: SearchAllRoomResultItemData) {
            this.item = item
            if (item.result.isNullOrEmpty()) {
                binding.rvResult.gone()
                binding.vErrorView.show(
                    errorEmptyResId = APP_R.drawable.common_list_empty_ic,
                    title = APP_R.string.commonui_list_empty
                )
            } else {
                binding.rvResult.show()
                resultAdapter.submitList(item.result)
                binding.vErrorView.gone()
            }
        }
    }

}