package com.adealink.weparty.applyskill

import android.os.Bundle
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.applyskill.step.ApplySKillFormFragment
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.JoinUsData
import com.adealink.weparty.joinus.data.JoinUsStep
import com.adealink.weparty.joinus.databinding.ActivityApplySkillBinding
import com.adealink.weparty.joinus.review.JoinUsStepReviewFragment
import com.adealink.weparty.joinus.step3.viewmodel.Step3ViewModel
import com.adealink.weparty.joinus.viewmodel.JoinUsViewModel
import com.adealink.weparty.joinus.viewmodel.JoinUsViewModelFactory
import com.adealink.weparty.module.joinus.JoinUs
import com.adealink.weparty.module.playmate.data.PlaymateCategoryData
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [JoinUs.ApplySKill.PATH],
    interceptors = [ApplySkillInterceptor::class],
    desc = "申请技能详情"
)
class ApplySkillActivity : BaseActivity() {

    @BindExtra(JoinUs.Common.EXTRA_JOIN_US)
    var joinUsData: JoinUsData? = null

    @BindExtra(JoinUs.ApplySKill.EXTRA_CATEGORY)
    var applyCategory: PlaymateCategoryData? = null

    private val binding by viewBinding(ActivityApplySkillBinding::inflate)

    private val viewModel by viewModels<JoinUsViewModel> { JoinUsViewModelFactory() }

    private val step3ViewModel by viewModels<Step3ViewModel> { JoinUsViewModelFactory() }

    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 loadData() {
        super.loadData()
        val joinUsData = joinUsData
        if (joinUsData != null) {
            viewModel.setJoinProgress(joinUsData)
            return
        }
        val applyCategory = applyCategory
        if (applyCategory != null) {
            step3ViewModel.selectCategory(applyCategory).observe(this) { rlt ->
                when (rlt) {
                    is Rlt.Failed -> {
                        showFailedToast(rlt)
                    }

                    is Rlt.Success -> {
                        viewModel.goStep(JoinUsStep.STEP_3)
                    }
                }
            }
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.joinProgressLD.observe(this) { progressData ->
            when (progressData?.getCurrentStep()) {
                JoinUsStep.UNDER_REVIEW -> {
                    viewModel.goStep(JoinUsStep.UNDER_REVIEW)
                }

                JoinUsStep.STEP_1,
                JoinUsStep.STEP_2,
                JoinUsStep.STEP_3 -> {
                    viewModel.goStep(JoinUsStep.STEP_3)
                }

                null -> {
                    viewModel.goStep(JoinUsStep.STEP_3)
                }
            }
        }
        viewModel.stepLD.observe(this) { step ->
            when (step) {
                JoinUsStep.UNDER_REVIEW -> {
                    inflateUnderReviewPage()
                }

                JoinUsStep.STEP_1,
                JoinUsStep.STEP_2,
                JoinUsStep.STEP_3 -> {
                    inflateStep3ReviewPage()
                }
            }
        }
    }

    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 inflateStep3ReviewPage() {
        var fragment = supportFragmentManager.findFragmentByTag(JoinUsStep.STEP_3.step)
        if (fragment != null && fragment.isAdded) {
            return
        }
        fragment = ApplySKillFormFragment().also {
            pageFragment = it
            it.arguments = Bundle().apply {
                putParcelable(JoinUs.ApplySKill.EXTRA_CATEGORY, applyCategory)
            }
        }
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, fragment, JoinUsStep.STEP_3.step)
            .commitAllowingStateLoss()
    }


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


}