package com.adealink.weparty.playmate.findpartner.page

import androidx.recyclerview.widget.GridLayoutManager
import com.adealink.frame.aab.util.getCompatColor
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
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.GridSpacingItemDecoration
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.databinding.FragmentFindPartnerCategoryBinding
import com.adealink.weparty.playmate.databinding.LayoutFindPartnerTabItemBinding
import com.adealink.weparty.playmate.findpartner.FindPartnerFragment
import com.adealink.weparty.playmate.findpartner.adapter.CategoryItemViewBinder
import com.adealink.weparty.playmate.findpartner.data.FindPartnerPage
import com.adealink.weparty.playmate.findpartner.viewmodel.FindPartnerViewModel
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
import com.google.android.material.tabs.TabLayout
import com.adealink.weparty.R as APP_R

class SelectCategoryFragment : BaseFragment(R.layout.fragment_find_partner_category),
    CategoryItemViewBinder.CategoryChooseListener {

    companion object {
        const val TAG = "SelectCategoryFragment"
    }

    private val binding by viewBinding(FragmentFindPartnerCategoryBinding::bind)

    private var tabList = listOf<CategoryListTitleData>()
    private val listAdapter by fastLazy { MultiTypeListAdapter<CategoryItemData>() }
    private val findPartnerViewModel by parentFragmentViewModels<FindPartnerViewModel, FindPartnerFragment>(
        FindPartnerFragment::class.java
    )

    override fun initViews() {
        super.initViews()
        binding.tlCategory.addOnTabSelectedListener(
            object : TabLayout.OnTabSelectedListener {

                override fun onTabSelected(tab: TabLayout.Tab?) {
                    onTabSelect(tab?.position ?: 0)
                    updateTabView(tab, true, tab?.position ?: 0)
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    updateTabView(tab, false, tab?.position ?: 0)
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {
                }

            }
        )

        listAdapter.register(CategoryItemViewBinder(this))
        binding.rvCategory.adapter = listAdapter
        binding.rvCategory.layoutManager = GridLayoutManager(context, 4)
        binding.rvCategory.addItemDecoration(
            GridSpacingItemDecoration(
                4, 0, 20.dp(), true,
                extraBottom = getCompatDimension(APP_R.dimen.main_bottom_bar_margin_bottom).toInt()
            )
        )
    }

    private fun updateTabView(
        tabView: TabLayout.Tab?,
        isSelected: Boolean,
        position: Int
    ) {
        tabView?.customView?.let {
            val customViewBinding = LayoutFindPartnerTabItemBinding.bind(it)
            customViewBinding.tvTab.text = tabList.getOrNull(position)?.data?.name ?: ""
            if (isSelected) {
                customViewBinding.root.setBackgroundResource(APP_R.drawable.common_tab_item_bg)
                customViewBinding.tvTab.setTextColor(getCompatColor(APP_R.color.color_FF1D2129))
            } else {
                customViewBinding.root.setBackgroundResource(0)
                customViewBinding.tvTab.setTextColor(getCompatColor(APP_R.color.color_FF4E5969))
            }
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        findPartnerViewModel.categoryTitleLD.observe(viewLifecycleOwner) {
            tabList = it
            onTabListChanged(it)
        }
    }

    private fun onTabListChanged(tabs: List<CategoryListTitleData>) {
        binding.tlCategory.removeAllTabs()
        tabs.forEachIndexed { position, data ->
            val tabView = binding.tlCategory.newTab()
            tabView.setCustomView(R.layout.layout_find_partner_tab_item)
            updateTabView(tabView, false, position)
            binding.tlCategory.addTab(tabView)
        }
        binding.tlCategory.getTabAt(0)?.let {
            binding.tlCategory.selectTab(it)
        }
    }

    private fun onTabSelect(positon: Int) {
        val tabData = tabList.getOrNull(positon)

        val itemList = tabData?.data?.subCategoryList?.map {
            CategoryListItemData(tabData.data.code, it)
        } ?: emptyList()

        listAdapter.submitList(itemList)
        findPartnerViewModel.selectFirstCode(tabData?.data?.code)
    }

    override fun onChoose(data: PlaymateCategoryData) {
        findPartnerViewModel.selectCategory(data)
        findPartnerViewModel.showPage(FindPartnerPage.SELECT_OTHER)
    }

}