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

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.VerticalSpaceItemDecoration
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.module.playmate.PlaymateModule
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.module.profile.data.EditUserInfo
import com.adealink.weparty.module.profile.data.UserInterest
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.DialogEditInterestBinding
import com.adealink.weparty.profile.edit.dialog.adapter.InterestListViewBinder
import com.adealink.weparty.profile.edit.dialog.adapter.InterestTitleViewBinder
import com.adealink.weparty.ui.category.data.CategoryItemData
import com.adealink.weparty.ui.category.data.CategoryListData
import com.adealink.weparty.ui.category.data.CategoryListTitleData
import com.adealink.weparty.R as APP_R

class EditInterestDialog : BottomDialogFragment(R.layout.dialog_edit_interest) {

    companion object {
        const val TAG = "EditInterestDialog"
    }

    private val binding by viewBinding(DialogEditInterestBinding::bind)

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

    private val categoryViewModel by fastLazy { PlaymateModule.getPlaymateViewModel(this) }
    private var interestList: List<String>? = null
    private var categoryList = listOf<CategoryItemData>()

    private var callback: IEditInterestCallback? = null

    fun setCallback(callback: IEditInterestCallback?) {
        this.callback = callback
    }

    fun setInterests(interests: List<UserInterest>?) {
        this.interestList = interests?.map { it.code }
    }

    override fun initViews() {
        super.initViews()
        binding.ivBack.onClick {
            dismiss()
        }
        binding.btnSave.onClick {
            save()
        }

        binding.rvCategory.adapter = listAdapter
        listAdapter.register(InterestTitleViewBinder())
        listAdapter.register(InterestListViewBinder {
            checkInterestCount().also { enable ->
                if (!enable) {
                    showToast(
                        getCompatString(
                            APP_R.string.common_cannot_select_more_interest,
                            EditUserInfo.MAX_INTEREST_COUNT.toString()
                        )
                    )
                }
            }
        })
        binding.rvCategory.layoutManager =
            LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvCategory.addItemDecoration(
            VerticalSpaceItemDecoration(10.dp(), 14.dp(), 14.dp())
        )
    }

    override fun loadData() {
        super.loadData()
        categoryViewModel?.pullAllCategory()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        categoryViewModel?.allCategoryLD?.observe(viewLifecycleOwner) {
            val onlineList = mutableListOf<PlaymateCategoryData>()
            val activityList = mutableListOf<PlaymateCategoryData>()
            for (entry in it) {
                if (entry.isActivity()) {
                    activityList.add(entry)
                } else {
                    onlineList.add(entry)
                }
            }

            val itemList = mutableListOf<CategoryItemData>()
            for (data in onlineList) {
                itemList.add(CategoryListTitleData(data))
                itemList.add(CategoryListData(data).apply {
                    selectedCodes.addAll(interestList ?: emptySet())
                })
            }
            for (data in activityList) {
                itemList.add(CategoryListTitleData(data))
                itemList.add(CategoryListData(data).apply {
                    selectedCodes.addAll(interestList ?: emptySet())
                })
            }
            submitCategoryList(itemList)
        }
    }

    private fun submitCategoryList(list: List<CategoryItemData>) {
        this.categoryList = list
        listAdapter.submitList(list)
    }

    private fun checkInterestCount(): Boolean {
        var count = 0
        for (data in categoryList) {
            if (data is CategoryListData) {
                count += data.getUserInterest().size
            }
        }
        return count < EditUserInfo.MAX_INTEREST_COUNT
    }

    private fun save() {
        val userInterests = mutableListOf<UserInterest>()
        for (data in categoryList) {
            if (data is CategoryListData) {
                userInterests.addAll(data.getUserInterest())
            }
        }
        callback?.onEditInterest(userInterests)
        dismiss()
    }

    interface IEditInterestCallback {
        fun onEditInterest(interests: List<UserInterest>?)
    }

}