package com.adealink.weparty.ui.category

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.statusBarHeight
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.databinding.ActivityCategoryBinding
import com.adealink.weparty.module.category.Category
import com.adealink.weparty.ui.home.HomeFragment

@RouterUri(path = [Category.CategoryList.PATH], desc = "品类列表")
class CategoryActivity : BaseActivity() {

    companion object {
        private const val TAG = "CategoryActivity"
    }

    private val binding by viewBinding(ActivityCategoryBinding::inflate)

    override fun onBeforeCreate() {
        super.onBeforeCreate()
        Router.bind(this)
    }

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = this@CategoryActivity.statusBarHeight()
        }
        inflateCategoryFragment()
    }

    private fun inflateCategoryFragment() {
        val categoryFragment =
            supportFragmentManager.findFragmentByTag(HomeFragment.TAG) as? CategoryFragment
        if (categoryFragment?.isAdded == true) {
            return
        }
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, CategoryFragment(), CategoryFragment.TAG)
            .commitAllowingStateLoss()
    }


}