package com.adealink.weparty.country

import androidx.fragment.app.viewModels
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.frame.util.onClick
import com.adealink.weparty.R
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.diffutil.BaseListDiffUtil
import com.adealink.weparty.commonui.scroller.SectionScrollAdapter
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.country.adapter.CountryItemViewBinder
import com.adealink.weparty.country.adapter.CountryTitleItemViewBinder
import com.adealink.weparty.country.data.BaseCountryItemData
import com.adealink.weparty.country.data.CountryData
import com.adealink.weparty.country.data.CountryItemData
import com.adealink.weparty.country.data.CountryTitleItemData
import com.adealink.weparty.country.viewmodel.CountryViewModel
import com.adealink.weparty.databinding.DialogSelectCountryBinding

class CountrySelectDialog : BottomDialogFragment(R.layout.dialog_select_country) {

    private val binding by viewBinding(DialogSelectCountryBinding::bind)

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

    private val countryViewModel by viewModels<CountryViewModel>()

    private var onItemSelectListener: ((item: CountryItemData) -> Unit)? = null

    private var titleToContent = mutableListOf<Int>()

    fun setOnItemSelect(select: ((item: CountryItemData) -> Unit)? = null) {
        this.onItemSelectListener = select
    }

    override fun initViews() {
        super.initViews()
        binding.ivClose.onClick {
            dismiss()
        }

        listAdapter.register(CountryTitleItemViewBinder())
        listAdapter.register(CountryItemViewBinder { item ->
            onItemSelectListener?.invoke(item)
            dismiss()
        })

        binding.rvCountry.adapter = listAdapter
        binding.rvCountry.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
    }

    override fun loadData() {
        super.loadData()
        countryViewModel.loadCountryList().observe(viewLifecycleOwner) {
            submitCountryList(it)
        }
    }

    private fun submitCountryList(countryList: List<CountryData>) {
        val titleItems = mutableListOf<CountryTitleItemData>()
        val items = mutableListOf<BaseCountryItemData>()
        var firstChar: String? = null
        titleToContent.clear()
        for ((index, data) in countryList.withIndex()) {
            val displayName = data.getDisplayName()
            val displayNameFirstChar = displayName.firstOrNull()?.toString()
            if (firstChar == null || firstChar != displayNameFirstChar) {
                firstChar = displayNameFirstChar

                val titleItem = CountryTitleItemData(firstChar ?: "")
                titleItems.add(titleItem)
                items.add(titleItem)
                titleToContent.add(index)
            }
            val contentItem = CountryItemData(data)
            items.add(contentItem)
        }

        listAdapter.submitList(items)
        binding.vScroller.setSectionScrollAdapter(object : SectionScrollAdapter {
            override fun getSectionCount(): Int {
                return titleItems.size
            }

            override fun getSectionTitle(position: Int): String? {
                return titleItems.getOrNull(position)?.name
            }

            override fun getSectionWeight(position: Int): Int {
                return 1
            }

        })
    }

}