package com.adealink.weparty.joinus

import android.view.KeyEvent
import androidx.activity.viewModels
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
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.ext.gone
import com.adealink.weparty.commonui.ext.show
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.joinus.comp.JoinStepProgressComp
import com.adealink.weparty.joinus.data.JoinUsData
import com.adealink.weparty.joinus.data.JoinUsStep
import com.adealink.weparty.joinus.databinding.ActivityJoinUsBinding
import com.adealink.weparty.joinus.review.JoinUsStepReviewFragment
import com.adealink.weparty.joinus.step1.JoinUsStep1Fragment
import com.adealink.weparty.joinus.step2.JoinUsStep2Fragment
import com.adealink.weparty.joinus.step3.JoinUsStep3Fragment
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.util.applyImmersive
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [JoinUs.JoinUs.PATH],
    interceptors = [JoinUsInterceptor::class],
    desc = "陪玩师入住"
)
class JoinUsActivity : BaseActivity() {

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

    private val binding by viewBinding(ActivityJoinUsBinding::inflate)

    private val viewModel by viewModels<JoinUsViewModel> { 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.updateLayoutParams<ConstraintLayout.LayoutParams> {
                topMargin = top
            }
        }
        binding.topBar.backCallback = {
            if (pageFragment?.onBackPressed() != true) {
                onBackPressedDispatcher.onBackPressed()
            }
        }
    }

    override fun initComponents() {
        super.initComponents()
        JoinStepProgressComp(this, binding.vProgress).attach()
    }

    override fun loadData() {
        super.loadData()
        val joinUsData = joinUsData
        if (joinUsData == null) {
            showLoading()
            viewModel.loadApplyProgress().observe(this) {
                dismissLoading()
                showFailedToast(it)
            }
        } else {
            viewModel.setJoinProgress(joinUsData)
        }
    }

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

                JoinUsStep.STEP_1 -> {
                    viewModel.goStep(JoinUsStep.STEP_1)
                }

                JoinUsStep.STEP_2 -> {
                    viewModel.goStep(JoinUsStep.STEP_2)
                }

                JoinUsStep.STEP_3 -> {
                    viewModel.goStep(JoinUsStep.STEP_3)
                }

                null -> {
                    viewModel.goStep(JoinUsStep.STEP_1)
                }
            }
        }
        viewModel.stepLD.observe(this) { step ->
            when (step) {
                JoinUsStep.UNDER_REVIEW -> {
                    binding.vProgress.root.gone()
                    binding.ivTopBg.setImageResource(R.drawable.join_us_under_review_top_bg)
                    binding.flContent.show()
                    binding.flContent.setBackgroundResource(0)
                    inflateUnderReviewPage()
                }

                JoinUsStep.STEP_1 -> {
                    binding.vProgress.root.show()
                    binding.ivTopBg.setImageResource(R.drawable.join_us_top_bg)
                    binding.flContent.show()
                    binding.flContent.setBackgroundResource(APP_R.drawable.common_bottom_dialog_bg)
                    inflateStep1ReviewPage()
                }

                JoinUsStep.STEP_2 -> {
                    binding.vProgress.root.show()
                    binding.ivTopBg.setImageResource(R.drawable.join_us_top_bg)
                    binding.flContent.show()
                    binding.flContent.setBackgroundResource(APP_R.drawable.common_bottom_dialog_bg)
                    inflateStep2ReviewPage()
                }

                JoinUsStep.STEP_3 -> {
                    binding.vProgress.root.show()
                    binding.ivTopBg.setImageResource(R.drawable.join_us_top_bg)
                    binding.flContent.show()
                    binding.flContent.setBackgroundResource(APP_R.drawable.common_bottom_dialog_bg)
                    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 inflateStep1ReviewPage() {
        var fragment = supportFragmentManager.findFragmentByTag(JoinUsStep.STEP_1.step)
        if (fragment != null && fragment.isAdded) {
            return
        }
        fragment = JoinUsStep1Fragment().also {
            pageFragment = it
        }
        supportFragmentManager.beginTransaction()
            .replace(binding.flContent.id, fragment, JoinUsStep.STEP_1.step)
            .commitAllowingStateLoss()
    }

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

    private fun inflateStep3ReviewPage() {
        var fragment = supportFragmentManager.findFragmentByTag(JoinUsStep.STEP_3.step)
        if (fragment != null && fragment.isAdded) {
            return
        }
        fragment = JoinUsStep3Fragment().also {
            pageFragment = it
        }
        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)
    }


}