package com.adealink.weparty.ui.category

import androidx.recyclerview.widget.GridLayoutManager
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.weparty.R
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.recycleview.itemdecoration.VerticalSpaceItemDecoration
import com.adealink.weparty.databinding.FragmentCategoryBinding
import com.adealink.weparty.module.playmate.PlaymateModule
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.ui.category.adapter.CategoryListItemViewBinder
import com.adealink.weparty.ui.category.adapter.CategoryListTitleViewBinder
import com.adealink.weparty.ui.category.adapter.CategoryTabTitleViewBinder
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 CategoryFragment : BaseFragment(R.layout.fragment_category) {

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

    private val binding by viewBinding(FragmentCategoryBinding::bind)

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

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

    private lateinit var tabLayoutManger: LinearLayoutManager
    private lateinit var listLayoutManger: GridLayoutManager
    private var categoryList = listOf<CategoryItemData>()
    override fun initViews() {
        super.initViews()
        initTab()
        initList()
    }

    private fun initTab() {
        binding.rvTab.adapter = tabAdapter
        tabAdapter.register(CategoryTabTitleViewBinder())
        tabLayoutManger = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
        binding.rvTab.layoutManager = tabLayoutManger
        binding.rvTab.addItemDecoration(
            VerticalSpaceItemDecoration(24.dp())
        )
        binding.rvTab.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {

            }
        })
    }

    private fun initList() {
        binding.rvList.adapter = listAdapter
        listAdapter.register(CategoryListTitleViewBinder())
        listAdapter.register(CategoryListItemViewBinder())
        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.rvList.layoutManager = listLayoutManger
        binding.rvList.addItemDecoration(
            GridSpacingMultiSpanSizeItemDecoration(SPAN_COUNT, 12.dp(), 10.dp(), false)
        )
        binding.rvList.addOnScrollListener(object : RecyclerView.OnScrollListener() {

            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {

            }
        })
    }


    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>) {
        tabAdapter.submitList(list.filter {
            it is CategoryListTitleData
        })
        this.categoryList = list
        listAdapter.submitList(list)
    }


}