package com.adealink.weparty.account.register.fragment

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.AppBase
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.AppModule
import com.adealink.weparty.account.R
import com.adealink.weparty.account.databinding.FragmentRegisterSelectCategoryBinding
import com.adealink.weparty.account.register.adapter.CategoryListItemViewBinder
import com.adealink.weparty.account.register.viewmodel.RegisterProfileViewModel
import com.adealink.weparty.account.viewModel.AccountViewModelFactory
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.recycleview.adapter.MultiTypeListAdapter
import com.adealink.weparty.commonui.recycleview.itemdecoration.GridSpacingMultiSpanSizeItemDecoration
import com.adealink.weparty.module.playmate.PlaymateModule
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.ui.category.adapter.CategoryListTitleViewBinder
import com.adealink.weparty.ui.category.data.CategoryItemData
import com.adealink.weparty.ui.category.data.CategoryListItemData
import com.adealink.weparty.ui.category.data.CategoryListTitleData

class SelectCategoryFragment : BaseFragment(R.layout.fragment_register_select_category),
    CategoryListItemViewBinder.OnSelectListener {

    companion object {
        const val TAG = "SelectCategoryFragment"
        private const val SPAN_COUNT = 3
    }

    private val binding by viewBinding(FragmentRegisterSelectCategoryBinding::bind)

    private val viewModel by activityViewModels<RegisterProfileViewModel> { AccountViewModelFactory() }

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

    private val categoryViewModel by fastLazy { PlaymateModule.getPlaymateViewModel(this) }

    private lateinit var listLayoutManger: GridLayoutManager

    private var categoryList = listOf<CategoryItemData>()
    override fun initViews() {
        super.initViews()
        binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = activity.statusBarHeight()
        }
        binding.topBar.backCallback = {
            activity?.onBackPressed()
        }
        binding.btnNext.text = getCompatString(R.string.account_register_go_home, AppBase.appName)
        binding.btnNext.onClick {
            nextStep()
        }

        binding.rvCategory.adapter = listAdapter
        listAdapter.register(CategoryListTitleViewBinder())
        listAdapter.register(CategoryListItemViewBinder(this))
        listLayoutManger = GridLayoutManager(context, SPAN_COUNT, RecyclerView.VERTICAL, false)
        listLayoutManger.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                val data = listAdapter.items.getOrNull(position)
                if (data is CategoryListTitleData) {
                    return SPAN_COUNT
                }
                return 1
            }
        }
        binding.rvCategory.layoutManager = listLayoutManger
        binding.rvCategory.addItemDecoration(
            GridSpacingMultiSpanSizeItemDecoration(SPAN_COUNT, 12.dp(), 8.dp(), false)
        )
    }

    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.entries) {
                if (entry.value.isActivity()) {
                    activityList.add(entry.value)
                } else {
                    onlineList.add(entry.value)
                }
            }
            val itemList = mutableListOf<CategoryItemData>()
            for (data in onlineList) {
                itemList.add(CategoryListTitleData(data))

                for (subData in data.subCategoryList) {
                    itemList.add(CategoryListItemData(subData))
                }
            }
            for (data in activityList) {
                itemList.add(CategoryListTitleData(data))

                for (subData in data.subCategoryList) {
                    itemList.add(CategoryListItemData(subData))
                }
            }
            submitCategoryList(itemList)
        }
    }

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

    private fun nextStep() {
        activity?.let { act ->
            Router.build(act, AppModule.Main.PATH).start()
            act.finish()
        }
    }

    override fun onSelect(item: CategoryListItemData, pos: Int) {
        item.selected = !item.selected
        listAdapter.notifyItemChanged(pos)
    }

}