package com.adealink.weparty.playmate.findpartner.page

import android.view.LayoutInflater
import androidx.appcompat.widget.LinearLayoutCompat
import com.adealink.frame.aab.util.getCompatString
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.module.playmate.Playmate
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.FragmentFindPartnerOtherBinding
import com.adealink.weparty.playmate.databinding.LayoutFindPartnerOptionBinding
import com.adealink.weparty.playmate.findpartner.FindPartnerFragment
import com.adealink.weparty.playmate.findpartner.comp.FindPartnerOptionsComp
import com.adealink.weparty.playmate.findpartner.data.OptionData
import com.adealink.weparty.playmate.findpartner.viewmodel.FindPartnerViewModel
import com.adealink.weparty.viewmodel.parentFragmentViewModels
import com.adealink.weparty.R as APP_R

class SelectOtherFragment : BaseFragment(R.layout.fragment_find_partner_other) {

    companion object {
        const val TAG = "SelectOtherFragment"
    }

    private val binding by viewBinding(FragmentFindPartnerOtherBinding::bind)

    private val findPartnerViewModel by parentFragmentViewModels<FindPartnerViewModel, FindPartnerFragment>(
        FindPartnerFragment::class.java
    )
    private val optionCompList = mutableListOf<FindPartnerOptionsComp>()
    private val otherOptionCompList = mutableListOf<FindPartnerOptionsComp>()

    private lateinit var genderOption: FindPartnerOptionsComp

    //    private lateinit var ageOption: FindPartnerOptionsComp
    private lateinit var starOption: FindPartnerOptionsComp
    private lateinit var priceOption: FindPartnerOptionsComp
    override fun initViews() {
        super.initViews()
        binding.btnReset.onClick {
            reset()
        }
        binding.btnSubmit.onClick {
            submit()
        }
    }

    override fun initComponents() {
        super.initComponents()
        FindPartnerOptionsComp(
            this,
            getCompatString(APP_R.string.common_gender),
            listOf(
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_unlimited),
                    GenderOption.UNLIMITED.value.toString(),
                    selected = true
                ),
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_male),
                    GenderOption.MALE.value.toString(),
                ),
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_female),
                    GenderOption.FEMALE.value.toString(),
                ),
            ),
            binding.vOptionGender
        ).also {
            genderOption = it
            optionCompList.add(it)
        }.attach()

//        FindPartnerOptionsComp(
//            this,
//            getCompatString(APP_R.string.common_age),
//            listOf(
//                OptionData(
//                    getCompatString(R.string.playmate_find_partner_option_unlimited),
//                    AgeOption.UNLIMITED.value, true
//                ),
//                OptionData(
//                    getCompatString(R.string.playmate_find_partner_option_age_15_25),
//                    AgeOption.AGE_15_25.value
//                ),
//                OptionData(
//                    getCompatString(R.string.playmate_find_partner_option_age_25_35),
//                    AgeOption.AGE_25_35.value
//                ),
//                OptionData(
//                    getCompatString(R.string.playmate_find_partner_option_age_35),
//                    AgeOption.AGE_35.value
//                )
//            ),
//            binding.vOptionAge
//        ).also { ageOption = it }.attach()

        FindPartnerOptionsComp(
            this,
            getCompatString(APP_R.string.common_star_rank_filter),
            listOf(
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_unlimited),
                    StarOption.DEFAULT.value.toString(),
                    selected = true
                ),
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_evaluate_high_low),
                    StarOption.DESC.value.toString()
                ),
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_evaluate_low_high),
                    StarOption.ASC.value.toString()
                ),
            ),
            binding.vOptionEvaluate
        ).also {
            starOption = it
            optionCompList.add(it)
        }.attach()

        FindPartnerOptionsComp(
            this,
            getCompatString(APP_R.string.common_price_rank_filter),
            listOf(
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_unlimited),
                    PriceRangeOption.UNLIMITED.value.toString(),
                    selected = true
                ),
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_price_0_1000),
                    PriceRangeOption.PRICE_0_1000.value.toString()
                ),
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_price_1000_1500),
                    PriceRangeOption.PRICE_1000_1500.value.toString()
                ),
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_price_1500_2500),
                    PriceRangeOption.PRICE_1500_2500.value.toString()
                ),
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_price_2500_5000),
                    PriceRangeOption.PRICE_2500_5000.value.toString()
                ),
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_price_5000),
                    PriceRangeOption.PRICE_5000.value.toString()
                ),
            ),
            binding.vOptionPrice
        ).also {
            priceOption = it
            optionCompList.add(it)
        }.attach()


        //添加额外的选项
        inflateOptions()
    }

    private fun inflateOptions() {
        val data = findPartnerViewModel.selectCategoryLD.value
        val condition = findPartnerViewModel.getCondition(data?.code) ?: return
        for (field in condition.filterFields) {
            if (field.options.isEmpty()) {
                continue
            }
            val optionBinding =
                LayoutFindPartnerOptionBinding.inflate(LayoutInflater.from(context), null, false)
            binding.llOption.addView(
                optionBinding.root,
                LinearLayoutCompat.LayoutParams(
                    LinearLayoutCompat.LayoutParams.MATCH_PARENT,
                    LinearLayoutCompat.LayoutParams.WRAP_CONTENT
                )
            )

            val options = mutableListOf<OptionData>()
            options.add(
                OptionData(
                    getCompatString(R.string.playmate_find_partner_option_unlimited),
                    null,
                    key = field.fieldCode,
                    selected = true
                )
            )
            options.addAll(
                field.options.map {
                    OptionData(
                        it.value, it.key,
                        key = field.fieldCode
                    )
                }
            )

            FindPartnerOptionsComp(
                this,
                field.name,
                options,
                optionBinding
            ).also {
                optionCompList.add(it)
                otherOptionCompList.add(it)
            }.attach()
        }
    }

    private fun reset() {
        for (comp in optionCompList) {
            comp.reset()
        }
    }

    private fun submit() {
        val act = activity ?: return
        val firstCode = findPartnerViewModel.firstCategoryCodeLD.value ?: return
        val category = findPartnerViewModel.selectCategoryLD.value ?: return
        Router.build(act, Playmate.List.PATH).apply {
            putExtra(Playmate.Common.EXTRA_FIRST_CATEGORY_CODE, firstCode)
            putExtra(Playmate.Common.EXTRA_CATEGORY, category)
            putExtra(
                Playmate.List.EXTRA_OPTION_GENDER,
                genderOption.getSelected()?.value?.toIntOrNull() ?: GenderOption.UNLIMITED.value
            )
//            .putExtra(Playmate.List.EXTRA_OPTION_AGE, ageOption.getSelected()?.value ?: AgeOption.UNLIMITED.value)
            putExtra(
                Playmate.List.EXTRA_OPTION_STAR,
                starOption.getSelected()?.value?.toIntOrNull() ?: StarOption.DEFAULT.value
            )
            putExtra(
                Playmate.List.EXTRA_OPTION_PRICE_RANGE,
                priceOption.getSelected()?.value?.toIntOrNull() ?: PriceRangeOption.UNLIMITED.value
            )

            val otherOptions = arrayListOf<OptionData>()
            for (option in otherOptionCompList) {
                val selected = option.getSelected() ?: continue
                otherOptions.add(selected)
            }
            putParcelableArrayListExtra(
                Playmate.List.EXTRA_OPTION_OTHER,
                otherOptions
            )
        }.start()
        findPartnerViewModel.dismiss()
    }

}