package com.adealink.weparty.playmate.list

import android.os.Bundle
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.base.fastLazy
import com.adealink.frame.log.Log
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.BindExtra
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.module.playmate.Playmate
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.module.playmate.data.TAG_PLAYMATE_LIST
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.databinding.FragmentPlaymateHomeListBinding
import com.adealink.weparty.playmate.list.comp.PlaymateTopCategoryComp
import com.adealink.weparty.R as APP_R

@RouterUri(path = [Playmate.HomeList.PATH], desc = "陪玩列表")
class PlaymateHomeListFragment : BaseFragment(R.layout.fragment_playmate_home_list) {

    override val isLazyInit: Boolean = true
    override val isLazyLoad: Boolean = true

    @BindExtra(Playmate.Common.EXTRA_CATEGORY)
    var category: PlaymateCategoryData? = null

    private val binding by viewBinding(FragmentPlaymateHomeListBinding::bind)

    private val listFragment: PlaymateListFragment by fastLazy { PlaymateListFragment() }
    private lateinit var topComp: PlaymateTopCategoryComp

    private var currentCategory: PlaymateCategoryData? = null //当前选择的子品类

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        if (category == null) {
            Log.i(TAG_PLAYMATE_LIST, "PlaymateHomeListFragment init fail, for categoryCode is null")
        }
        inflatePlaymateList()
    }

    override fun initComponents() {
        super.initComponents()
        PlaymateTopCategoryComp(this, binding.vListTop, category) {
            selectCategory(it)
        }.also {
            topComp = it
        }.attach()
    }

    override fun loadData() {
        super.loadData()
        selectCategory(category?.subCategoryList?.firstOrNull())
    }

    private fun selectCategory(selected: PlaymateCategoryData?) {
        currentCategory = if (currentCategory == selected) {
            //取消勾选
            null
        } else {
            selected
        }
        topComp.selectCategory(currentCategory)
        listFragment.updateCategory(currentCategory)
    }

    private fun inflatePlaymateList() {
        if (listFragment.isAdded) {
            return
        }
        listFragment.arguments = (this@PlaymateHomeListFragment.arguments ?: Bundle()).apply {
            putInt(
                Playmate.Common.EXTRA_LAST_ITEM_BOTTOM,
                getCompatDimension(APP_R.dimen.main_content_padding_bottom).toInt()
            )
        }
        childFragmentManager.beginTransaction()
            .replace(binding.flContent.id, listFragment, Playmate.List.PATH)
            .commitAllowingStateLoss()
    }

}