package com.adealink.weparty.joinus.step3.page

import androidx.recyclerview.widget.GridLayoutManager
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.naviBarHeight
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.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.joinus.R
import com.adealink.weparty.joinus.databinding.FragmentJoinUsStep3ChooseSkillBinding
import com.adealink.weparty.joinus.step3.JoinUsStep3Fragment
import com.adealink.weparty.joinus.step3.data.Step3Page
import com.adealink.weparty.joinus.step3.viewmodel.Step3ViewModel
import com.adealink.weparty.module.playmate.PlaymateModule
import com.adealink.weparty.ui.category.adapter.CategoryListItemViewBinder
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
import com.adealink.weparty.viewmodel.parentFragmentViewModels

class ChooseSkillFragment : BaseFragment(R.layout.fragment_join_us_step_3_choose_skill) {

    companion object {
        const val TAG = "ChooseSkillFragment"
        private const val SPAN_COUNT = 4
    }


    private val binding by viewBinding(FragmentJoinUsStep3ChooseSkillBinding::bind)

    private val step3ViewModel by parentFragmentViewModels<Step3ViewModel, JoinUsStep3Fragment>(
        JoinUsStep3Fragment::class.java
    )

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

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

    override fun initViews() {
        super.initViews()
        binding.rvSkill.adapter = listAdapter
        listAdapter.register(CategoryListTitleViewBinder())
        listAdapter.register(CategoryListItemViewBinder { item, position ->
            selectItem(item)
        })
        val 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.rvSkill.layoutManager = listLayoutManger
        binding.rvSkill.addItemDecoration(
            GridSpacingMultiSpanSizeItemDecoration(SPAN_COUNT, 11.dp(), 8.dp(), false)
        )
        binding.rvSkill.setPadding(0, 0, 0, activity.naviBarHeight() + 24.dp())
    }

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

    override fun observeViewModel() {
        super.observeViewModel()
        categoryViewModel?.allCategoryLD?.observe(viewLifecycleOwner) { categoryList ->
            dismissLoading()
            val itemList = mutableListOf<CategoryItemData>()
            for (data in categoryList) {
                itemList.add(CategoryListTitleData(data))

                for (subData in data.subCategoryList) {
                    itemList.add(CategoryListItemData(data.code, subData))
                }
            }
            listAdapter.submitList(itemList)
        }
    }


    private fun selectItem(item: CategoryListItemData) {
        showLoading()
        step3ViewModel.selectCategory(item.data).observe(viewLifecycleOwner) { rlt ->
            dismissLoading()
            when (rlt) {
                is Rlt.Failed -> {
                    showFailedToast(rlt)
                }

                is Rlt.Success -> {
                    val fields = rlt.data.fields
                    if (fields.isEmpty()) {
                        showToast(R.string.join_us_category_form_is_empty)
                        return@observe
                    }
                    step3ViewModel.showPage(Step3Page.FILL_FORM)
                }
            }
        }
    }


}