package com.adealink.weparty.room.member

import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.room.R
import com.adealink.weparty.room.databinding.DialogRoomPlaymateCategoryFilterBinding
import com.adealink.weparty.room.member.adapter.RoomPlaymateAllCategoryItemBinder
import com.adealink.weparty.room.member.adapter.RoomPlaymateCategoryItemBinder
import com.adealink.weparty.room.member.data.BasePlaymateCategoryItem
import com.adealink.weparty.room.member.data.PlaymateCategoryAllItemData
import com.adealink.weparty.room.member.data.PlaymateCategoryItemData
import com.adealink.weparty.room.member.viewmodel.RoomPlaymateSkillViewModel

class RoomPlaymateFilterDialog : BottomDialogFragment(R.layout.dialog_room_playmate_category_filter) {

    private val binding by viewBinding(DialogRoomPlaymateCategoryFilterBinding::bind)
    private val categoryAdapter by fastLazy { MultiTypeListAdapter<BasePlaymateCategoryItem>() }

    private val viewModel by activityViewModels<RoomPlaymateSkillViewModel>()

    private var listener: OnCategorySelectListener? = null

    private var currentSkillId: String? = null

    fun setListener(listener: OnCategorySelectListener?) {
        this.listener = listener
    }

    fun setCurrentSelect(skillId: String?) {
        this.currentSkillId = skillId
    }

    override fun initViews() {
        super.initViews()
        categoryAdapter.register(
            RoomPlaymateCategoryItemBinder {
                select(it.skill)
            }
        )
        categoryAdapter.register(
            RoomPlaymateAllCategoryItemBinder {
                selectAll()
            }
        )
        binding.rvCategory.adapter = categoryAdapter
        binding.rvCategory.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
    }

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.skillListLD.observe(viewLifecycleOwner) { list ->
            val itemList = mutableListOf<BasePlaymateCategoryItem>()
            var hasSelected = false
            itemList.addAll(
                list.map { skill ->
                    PlaymateCategoryItemData(
                        skill
                    ).also {
                        hasSelected = it.skill.code == currentSkillId
                        it.selected = hasSelected
                    }
                }
            )
            itemList.add(
                PlaymateCategoryAllItemData().also {
                    it.selected = !hasSelected && currentSkillId.isNullOrEmpty()
                }
            )
            categoryAdapter.submitList(itemList)
        }
    }

    override fun loadData() {
        super.loadData()
        viewModel.loadRoomPlaymateSkill().observe(viewLifecycleOwner) {
            showFailedToast(it)
        }
    }

    private fun select(skill: PlaymateCategoryData) {
        listener?.onSelect(skill)
        dismiss()
    }

    private fun selectAll() {
        listener?.onSelectAll()
        dismiss()
    }


    interface OnCategorySelectListener {
        fun onSelect(skill: PlaymateCategoryData)

        fun onSelectAll()
    }

}