package com.adealink.weparty.playmate.findpartner

import android.app.Dialog
import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.viewModels
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.base.fastLazy
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.frame.util.DisplayUtil
import com.adealink.frame.util.onClick
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.playmate.R
import com.adealink.weparty.playmate.databinding.FragmentFindPartnerBinding
import com.adealink.weparty.playmate.findpartner.data.FindPartnerPage
import com.adealink.weparty.playmate.findpartner.page.SelectCategoryFragment
import com.adealink.weparty.playmate.findpartner.page.SelectOtherFragment
import com.adealink.weparty.playmate.findpartner.viewmodel.FindPartnerViewModel
import com.adealink.weparty.playmate.viewmodel.PlaymateViewModel
import com.adealink.weparty.ui.category.data.CategoryListTitleData
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.google.android.material.R as M_R

@RouterUri(path = [Playmate.FindPartner.PATH], desc = "Find Partner")
class FindPartnerFragment : BottomSheetDialogFragment(R.layout.fragment_find_partner) {

    private val binding by viewBinding(FragmentFindPartnerBinding::bind)

    private var page = FindPartnerPage.SELECT_CATEGORY

    private val selectCategory by fastLazy { SelectCategoryFragment() }
    private val selectOther by fastLazy { SelectOtherFragment() }
    private val findPartnerViewModel by viewModels<FindPartnerViewModel>()
    private val categoryViewModel by viewModels<PlaymateViewModel>()

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return BottomSheetDialog(requireActivity(), theme).apply {
            setContentView(R.layout.fragment_find_partner)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.findViewById<View>(M_R.id.design_bottom_sheet)?.let {
            it.background = null
        }
        initViews()
        observeViewModel()
        loadData()
    }


    private fun initViews() {
        binding.fcFragment.updateLayoutParams<ConstraintLayout.LayoutParams> {
            height = (DisplayUtil.getScreenHeight() * 0.48f).toInt()
            bottomMargin =
                getCompatDimension(com.adealink.weparty.R.dimen.main_bottom_bar_margin_bottom).toInt()
        }
        binding.ivBack.onClick {
            clickBack()
        }
        inflatePage(FindPartnerPage.SELECT_CATEGORY)
    }

    private fun observeViewModel() {
        findPartnerViewModel.pageLD.observeWithoutCache(viewLifecycleOwner) {
            inflatePage(it)
        }
        findPartnerViewModel.dismissLD.observeWithoutCache(viewLifecycleOwner) {
            dismiss()
        }
        categoryViewModel.allCategoryLD.observe(viewLifecycleOwner) { categoryList ->
            findPartnerViewModel.setCategoryList(
                categoryList.map { CategoryListTitleData(it) }
            )
        }
        categoryViewModel.allCategoryConditionLD.observe(viewLifecycleOwner) { condition ->
            findPartnerViewModel.setCondition(condition)
        }
    }

    private fun inflatePage(page: FindPartnerPage) {
        this.page = page
        when (page) {
            FindPartnerPage.SELECT_CATEGORY -> {
                binding.ivBack.gone()
                childFragmentManager.beginTransaction()
                    .replace(binding.fcFragment.id, selectCategory, SelectCategoryFragment.TAG)
                    .commitAllowingStateLoss()
            }

            FindPartnerPage.SELECT_OTHER -> {
                binding.ivBack.show()
                childFragmentManager.beginTransaction()
                    .replace(binding.fcFragment.id, selectOther, SelectOtherFragment.TAG)
                    .commitAllowingStateLoss()
            }
        }
    }

    private fun loadData() {
        categoryViewModel.pullAllCategoryCondition()
    }

    private fun clickBack() {
        when (page) {
            FindPartnerPage.SELECT_CATEGORY -> {
                //
            }

            FindPartnerPage.SELECT_OTHER -> {
                inflatePage(FindPartnerPage.SELECT_CATEGORY)
            }
        }
    }


}