package com.adealink.weparty.playmatesetting

import android.view.KeyEvent
import androidx.activity.viewModels
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.adealink.frame.aab.util.getCompatDimension
import com.adealink.frame.base.Rlt
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.commonui.BaseFragment
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.joinus.data.JoinUsStep
import com.adealink.weparty.joinus.databinding.ActivityPlaymateSettingBinding
import com.adealink.weparty.joinus.review.JoinUsStepReviewFragment
import com.adealink.weparty.joinus.viewmodel.JoinUsViewModel
import com.adealink.weparty.joinus.viewmodel.JoinUsViewModelFactory
import com.adealink.weparty.module.playmate.Playmate
import com.adealink.weparty.module.playmate.data.PlaymateDetailData
import com.adealink.weparty.playmatesetting.page.PlaymateSettingFormFragment
import com.adealink.weparty.playmatesetting.viewmodel.PlaymateSettingModelFactory
import com.adealink.weparty.playmatesetting.viewmodel.PlaymateSettingViewModel
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [Playmate.PlaymateSetting.PATH],

    desc = "陪玩师技能修改详情"
)
class PlaymateSettingActivity : BaseActivity() {

    @BindExtra(Playmate.PlaymateSetting.EXTRA_PLAYMATE)
    var playmateData: PlaymateDetailData? = null

    private val binding by viewBinding(ActivityPlaymateSettingBinding::inflate)

    private val joinUsViewModel by viewModels<JoinUsViewModel> { JoinUsViewModelFactory() }
    private val settingViewModel by viewModels<PlaymateSettingViewModel> { PlaymateSettingModelFactory() }

    private var pageFragment: BaseFragment? = null
    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 = getCompatDimension(APP_R.dimen.common_top_bar_height).toInt() + top
            }
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        joinUsViewModel.joinProgressLD.observe(this) { joinUsData ->
            when (joinUsData?.getCurrentStep()) {
                JoinUsStep.UNDER_REVIEW -> {
                    inflateUnderReviewPage()
                }

                JoinUsStep.STEP_1,
                JoinUsStep.STEP_2,
                JoinUsStep.STEP_3,
                null -> {
                    val playmateData = this@PlaymateSettingActivity.playmateData
                    if (playmateData == null) {
                        return@observe
                    }
                    settingViewModel.selectCategory(playmateData).observe(this) { rlt ->
                        dismissLoading()
                        when (rlt) {
                            is Rlt.Failed -> {
                                showFailedToast(rlt)
                            }

                            is Rlt.Success -> {
                                inflateFormPage()
                            }
                        }
                    }
                }
            }
        }
        settingViewModel.goUnderReviewPageLD.observeWithoutCache(this) {
            inflateUnderReviewPage()
        }
        settingViewModel.submitFinishLD.observeWithoutCache(this) {
            finish()
        }
    }

    override fun loadData() {
        super.loadData()
        val playmateData = this@PlaymateSettingActivity.playmateData
        if (playmateData == null) {
            return
        }
        showLoading()
        joinUsViewModel.loadApplyProgress().observe(this) { rlt ->
            showFailedToast(rlt)
            dismissLoading()
        }
    }

    private fun inflateUnderReviewPage() {
        var fragment = supportFragmentManager.findFragmentByTag(JoinUsStep.UNDER_REVIEW.step)
        if (fragment != null && fragment.isAdded) {
            return
        }
        fragment = JoinUsStepReviewFragment().also {
            pageFragment = it
        }
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, fragment, JoinUsStep.UNDER_REVIEW.step)
            .commitAllowingStateLoss()
    }

    private fun inflateFormPage() {
        var fragment = supportFragmentManager.findFragmentByTag("PlaymateSettingFormFragment")
        if (fragment != null && fragment.isAdded) {
            return
        }
        fragment = PlaymateSettingFormFragment().also {
            pageFragment = it
        }
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, fragment, "PlaymateSettingFormFragment")
            .commitAllowingStateLoss()
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (pageFragment?.onKeyDown(keyCode, event) == true) {
            return true
        }
        return super.onKeyDown(keyCode, event)
    }


}