package com.adealink.weparty.joinus.step2.dialog

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.base.Rlt
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
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.joinus.R
import com.adealink.weparty.joinus.databinding.DialogJoinUsStep2LanguageBinding
import com.adealink.weparty.joinus.step2.JoinUsStep2Fragment
import com.adealink.weparty.joinus.step2.data.LanguageConfigData
import com.adealink.weparty.joinus.step2.data.LanguageItemData
import com.adealink.weparty.joinus.step2.viewmodel.Step2ViewModel
import com.adealink.weparty.viewmodel.parentFragmentViewModels

class ChooseLanguageDialog : BottomDialogFragment(R.layout.dialog_join_us_step2_language),
    LanguageItemViewBinder.OnLanguageChooseListener {

    private val binding by viewBinding(DialogJoinUsStep2LanguageBinding::bind)
    private val listAdapter by fastLazy { MultiTypeListAdapter<LanguageItemData>() }

    private val step2ViewModel by parentFragmentViewModels<Step2ViewModel, JoinUsStep2Fragment>(
        JoinUsStep2Fragment::class.java
    )

    private var chooseCallback: ((list: List<LanguageConfigData>) -> Unit)? = null

    fun setChooseCallback(callback: ((list: List<LanguageConfigData>) -> Unit)? = null) {
        this.chooseCallback = callback
    }


    private var languageItemList = listOf<LanguageItemData>()

    private var selectedLanguageCodes = mutableSetOf<String>() //
    fun setLanguageItemList(list: List<LanguageConfigData>?) {
        this.selectedLanguageCodes.clear()
        this.selectedLanguageCodes.addAll(list?.map { it.code } ?: emptyList())
    }

    override fun initViews() {
        super.initViews()
        binding.rvLanguages.adapter = listAdapter
        listAdapter.register(LanguageItemViewBinder(this))
        binding.rvLanguages.layoutManager =
            LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.btnConfirm.onClick {
            clickConfirm()
        }
    }

    override fun loadData() {
        super.loadData()
        step2ViewModel.loadLanguageConfig().observe(viewLifecycleOwner) { rlt ->
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

                is Rlt.Success -> {
                    val list = rlt.data
                    val itemList = list.mapIndexed { index, data ->
                        LanguageItemData(
                            data,
                            index < list.size - 1,
                            selectedLanguageCodes.contains(data.code)
                        )
                    }
                    languageItemList = itemList
                    listAdapter.submitList(languageItemList)
                }
            }
        }
    }


    override fun onChoose(language: LanguageConfigData, position: Int) {
        languageItemList.getOrNull(position)?.let {
            it.selected = !it.selected
            if (it.selected) {
                selectedLanguageCodes.add(it.language.code)
            } else {
                selectedLanguageCodes.remove(it.language.code)
            }
        }
        listAdapter.notifyItemChanged(position)
    }

    fun clickConfirm() {
        val selectedList = languageItemList.filter { it.selected }.map { it.language }
        chooseCallback?.invoke(selectedList)
        dismiss()
    }

}