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

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
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.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.LayoutSearchAllUserBinding
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 SearchAllUserItemViewBinder(
    private val listener: SearchUserItemViewBinder.OnUserResultClickListener,
    private val clickMore: () -> Unit
) :
    ItemViewBinder<SearchAllUserResultItemData, SearchAllUserItemViewBinder.ViewHolder>() {

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

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

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

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

        private var item: SearchAllUserResultItemData? = null
        private var exposureReport = object : PlaymateListEventReporter() {
            override fun getReportEvent(
                viewHolder: RecyclerView.ViewHolder,
                position: Int
            ): BaseStatEvent? {
                val item = (resultAdapter.getItem(position) as? SearchUserResultItemData) ?: return null
                return PlaymateStatEvent(PlaymateStatEvent.Action.EXPOSURE).apply {
                    id to item.data.uid
                }
            }

            override fun reportEvent(events: List<BaseStatEvent>) {
                val reportIds = mutableListOf<String>()
                for (event in events) {
                    val playmateEvent = (event as? PlaymateStatEvent) ?: continue
                    val idValue = (playmateEvent.id.value as? String) ?: continue
                    reportIds.add(idValue)
                }
                if (reportIds.isNotEmpty()) {
                    PlaymateStatEvent(PlaymateStatEvent.Action.EXPOSURE).apply {
                        ids to reportIds
                    }.send()
                }
            }
        }

        fun initView() {
            resultAdapter.register(SearchUserItemViewBinder(listener))
            binding.rvResult.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            binding.rvResult.adapter = resultAdapter
            binding.rvResult.addItemDecoration(
                VerticalSpaceItemDecoration(20.dp())
            )
            exposureReport.register(binding.rvResult)

            binding.btnMore.onClick {
                clickMore.invoke()
            }
        }

        fun update(item: SearchAllUserResultItemData) {
            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()
            }
        }
    }

}