package com.adealink.weparty.profile.edit.dialog.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import com.adealink.frame.base.fastLazy
import com.adealink.weparty.commonui.ext.dp
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.layoutmanager.FlowLayoutManager
import com.adealink.weparty.profile.databinding.LayoutEditProfileInterestListBinding
import com.adealink.weparty.ui.category.data.CategoryListData
import com.adealink.weparty.ui.category.data.CategoryListItemData

class InterestListViewBinder(
    private val checkInterestCount: () -> Boolean
) : ItemViewBinder<CategoryListData, InterestListViewBinder.ViewHolder>() {

    override fun onBindViewHolder(
        holder: ViewHolder,
        item: CategoryListData,
    ) {
        holder.update(item)
    }

    override fun onCreateViewHolder(
        inflater: LayoutInflater,
        parent: ViewGroup,
    ): ViewHolder {
        return ViewHolder(
            LayoutEditProfileInterestListBinding.inflate(inflater, parent, false),
            checkInterestCount
        )
    }

    class ViewHolder(
        rootBinding: LayoutEditProfileInterestListBinding,
        private val checkInterestCount: () -> Boolean
    ) : BindingViewHolder<LayoutEditProfileInterestListBinding>(rootBinding) {

        private val listAdapter by fastLazy { MultiTypeListAdapter<CategoryListItemData>() }

        private var itemData: CategoryListData? = null

        init {
            listAdapter.register(InterestItemViewBinder({ item, position ->
                onSelected(item, position)
            }))
            rootBinding.rvInterest.adapter = listAdapter

            binding.rvInterest.layoutManager = FlowLayoutManager(
                FlowLayoutManager.VERTICAL,
                8.dp(),
                10.dp()
            )
        }

        fun update(item: CategoryListData) {
            itemData = item
            val items = item.data.subCategoryList.map {
                CategoryListItemData(
                    item.data.code,
                    it,
                    selected = item.selectedCodes.contains(it.code)
                )
            }
            listAdapter.submitList(items)
        }

        private fun onSelected(item: CategoryListItemData, position: Int) {
            val nextSelected = !item.selected
            if (nextSelected) {
                if (!checkInterestCount.invoke()) {
                    return
                }
            }
            item.selected = nextSelected
            if (item.selected) {
                itemData?.selectedCodes?.add(item.data.code)
            } else {
                itemData?.selectedCodes?.remove(item.data.code)
            }
            listAdapter.notifyItemChanged(position)
        }
    }

}