package com.adealink.weparty.playmate.list

import android.os.Bundle
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.commonui.ext.gone
import com.adealink.weparty.commonui.ext.show
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.GuestActivityPlaymateTopCategoryComp
import com.adealink.weparty.playmate.list.comp.GuestPlaymateTopCategoryComp

@RouterUri(path = [Playmate.HomeList.GUEST_PATH], desc = "(游客)陪玩列表")
class GuestPlaymateHomeListFragment : 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: GuestPlaymateListFragment by fastLazy { GuestPlaymateListFragment() }
    private var topComp: GuestPlaymateTopCategoryComp? = null
    private var activityTopComp: GuestActivityPlaymateTopCategoryComp? = null

    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()
        if (category?.isActivity() == true) {
            binding.vActivityListTop.root.show()
            binding.vListTop.root.gone()
            GuestActivityPlaymateTopCategoryComp(this, binding.vActivityListTop, category) {
                selectCategory(it)
            }.also {
                activityTopComp = it
            }.attach()
        } else {
            binding.vActivityListTop.root.gone()
            binding.vListTop.root.show()
            GuestPlaymateTopCategoryComp(this, binding.vListTop, category) {
                selectCategory(it)
            }.also {
                topComp = it
            }.attach()
        }
    }

    override fun loadData() {
        super.loadData()
        selectCategory(null)
    }

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

    private fun inflatePlaymateList() {
        if (listFragment.isAdded) {
            return
        }
        listFragment.arguments = Bundle().apply {
            putString(
                Playmate.Common.EXTRA_FIRST_CATEGORY_CODE,
                category?.code
            )
        }
        childFragmentManager.beginTransaction()
            .replace(binding.flContent.id, listFragment, "GuestPlaymateHomeList")
            .commitAllowingStateLoss()
    }

}