package com.adealink.weparty.playmate.list

import android.content.Intent
import android.os.Bundle
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatDimension
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.module.playmate.Playmate
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.data.GenderOption
import com.adealink.weparty.playmate.data.PriceRangeOption
import com.adealink.weparty.playmate.data.StarOption
import com.adealink.weparty.playmate.databinding.ActivityPlaymateListBinding
import com.adealink.weparty.playmate.findpartner.data.OptionData
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.util.parcelableExtra
import com.adealink.weparty.R as APP_R

@RouterUri(path = [Playmate.List.PATH], desc = "陪玩列表")
class PlaymateListActivity : BaseActivity() {

    @BindExtra(Playmate.Common.EXTRA_FIRST_CATEGORY_CODE)
    var firstCategoryCode: String? = null

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

    @BindExtra(Playmate.List.EXTRA_TITLE)
    var title: String? = null

    @BindExtra(Playmate.List.EXTRA_OPTION_GENDER)
    var optionGender: Int = GenderOption.UNLIMITED.value

//    @BindExtra(Playmate.List.EXTRA_OPTION_AGE)
//    var optionAge: Int = AgeOption.UNLIMITED.value

    @BindExtra(Playmate.List.EXTRA_OPTION_STAR)
    var optionStar: Int = StarOption.DEFAULT.value

    @BindExtra(Playmate.List.EXTRA_OPTION_PRICE_RANGE)
    var optionPrice: Int = PriceRangeOption.UNLIMITED.value

    @BindExtra(Playmate.List.EXTRA_OPTION_OTHER)
    var otherOptions: List<OptionData>? = null

    private val binding by viewBinding(ActivityPlaymateListBinding::inflate)
    private val listFragment: PlaymateListFragment by fastLazy { PlaymateListFragment() }

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

    override fun initViews() {
        super.initViews()
        setContentView(binding.root)
        applyImmersive { top ->
            binding.topBar.setPadding(0, top, 0, 0)
            binding.topBar.updateLayoutParams<ConstraintLayout.LayoutParams> {
                height = top + getCompatDimension(APP_R.dimen.common_top_bar_height).toInt()
            }
        }
        title?.let {
            binding.topBar.setTitle(it)
        } ?: let {
            binding.topBar.setTitle(getCompatString(R.string.playmate_list_title))
        }
        inflatePlaymateList()
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        firstCategoryCode = intent.getStringExtra(Playmate.Common.EXTRA_FIRST_CATEGORY_CODE)
        category =
            intent.parcelableExtra(Playmate.Common.EXTRA_CATEGORY, PlaymateCategoryData::class.java)

        title = intent.getStringExtra(Playmate.List.EXTRA_TITLE)
        title?.let {
            binding.topBar.setTitle(it)
        } ?: let {
            binding.topBar.setTitle(getCompatString(R.string.playmate_list_title))
        }
        optionGender =
            intent.getIntExtra(Playmate.List.EXTRA_OPTION_GENDER, GenderOption.UNLIMITED.value)
//        optionAge = intent.getIntExtra(Playmate.List.EXTRA_OPTION_AGE, AgeOption.UNLIMITED.value)
        optionStar = intent.getIntExtra(Playmate.List.EXTRA_OPTION_STAR, StarOption.DEFAULT.value)
        optionPrice =
            intent.getIntExtra(
                Playmate.List.EXTRA_OPTION_PRICE_RANGE,
                PriceRangeOption.UNLIMITED.value
            )
        otherOptions = intent.getParcelableArrayListExtra(Playmate.List.EXTRA_OPTION_OTHER)
        if (listFragment.isAdded) {
            listFragment.onNewIntent(intent)
        }
    }

    private fun inflatePlaymateList() {
        if (listFragment.isAdded) {
            return
        }
        listFragment.arguments = Bundle().apply {
            putString(
                Playmate.Common.EXTRA_FIRST_CATEGORY_CODE,
                firstCategoryCode
            )
            putParcelable(
                Playmate.Common.EXTRA_CATEGORY,
                category
            )
            putInt(Playmate.List.EXTRA_OPTION_GENDER, optionGender)
//            putInt(Playmate.List.EXTRA_OPTION_AGE, optionAge)
            putInt(Playmate.List.EXTRA_OPTION_STAR, optionStar)
            putInt(Playmate.List.EXTRA_OPTION_PRICE_RANGE, optionPrice)
            otherOptions?.let {
                putParcelableArrayList(
                    Playmate.List.EXTRA_OPTION_OTHER,
                    arrayListOf<OptionData>().apply {
                        addAll(it)
                    })
            }
        }
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, listFragment, "PlaymateHomeList")
            .commitAllowingStateLoss()
    }


}