package com.adealink.weparty.ui.category

import android.content.Intent
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.base.fastLazy
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.BaseActivity
import com.adealink.weparty.databinding.ActivityCategoryBinding
import com.adealink.weparty.module.category.Category
import com.adealink.weparty.module.category.data.CategoryAction
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.R as APP_R

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

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

    @BindExtra(Category.CategoryList.EXTRA_TITLE)
    var title: String? = null

    @BindExtra(Category.CategoryList.EXTRA_HIDDEN_CATEGORY)
    var hiddenList: List<String>? = null

    @BindExtra(Category.CategoryList.EXTRA_SELECT_ACTION)
    var selectAction: Int? = null

    private val binding by viewBinding(ActivityCategoryBinding::inflate)

    private val categoryFragment by fastLazy { CategoryFragment() }

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

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        applyImmersive { top ->
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }
        binding.topBar.setTitle(
            title ?: getCompatString(APP_R.string.common_all_category)
        )

        inflateCategoryFragment()
    }

    override fun handleNewIntent(intent: Intent?) {
        super.handleNewIntent(intent)
        title = intent?.getStringExtra(Category.CategoryList.EXTRA_TITLE)
        hiddenList = intent?.getStringArrayListExtra(
            Category.CategoryList.EXTRA_HIDDEN_CATEGORY
        )
        selectAction = intent?.getIntExtra(
            Category.CategoryList.EXTRA_SELECT_ACTION,
            CategoryAction.FIND_PLAYMATE.action
        )
        categoryFragment.onNewIntent(intent)
    }

    private fun inflateCategoryFragment() {
        if (categoryFragment.isAdded) {
            return
        }
        categoryFragment.arguments = Bundle().apply {
            putInt(
                Category.CategoryList.EXTRA_SELECT_ACTION,
                selectAction ?: CategoryAction.FIND_PLAYMATE.action
            )
            putStringArrayList(
                Category.CategoryList.EXTRA_HIDDEN_CATEGORY,
                arrayListOf<String>().apply {
                    addAll(hiddenList ?: emptyList())
                }
            )
        }
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, categoryFragment, CategoryFragment.TAG)
            .commitAllowingStateLoss()
    }


}