package com.adealink.weparty.playmate.saywelcome

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.aab.util.getCompatString
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.router.Router
import com.adealink.frame.router.annotation.RouterUri
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.dialogfragment.CommonTextActionDialog
import com.adealink.weparty.commonui.toast.util.showFailedToast
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.module.playmate.Playmate
import com.adealink.weparty.module.playmate.data.PLAYMATE_WELCOME_TEXT
import com.adealink.weparty.module.playmate.data.PLAYMATE_WELCOME_VOICE
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.databinding.ActivitySayWelcomeBinding
import com.adealink.weparty.playmate.saywelcome.data.MAX_WELCOME_COUNT
import com.adealink.weparty.playmate.saywelcome.data.onWelcomeAddLD
import com.adealink.weparty.playmate.saywelcome.data.onWelcomeDeleteLD
import com.adealink.weparty.playmate.saywelcome.data.onWelcomeUpdateLD
import com.adealink.weparty.playmate.saywelcome.fragment.EmptyWelcomeFragment
import com.adealink.weparty.playmate.saywelcome.fragment.WelcomeListFragment
import com.adealink.weparty.playmate.saywelcome.viewmodel.WelcomePageViewModel
import com.adealink.weparty.playmate.saywelcome.viewmodel.WelcomeViewModel
import com.adealink.weparty.util.applyImmersive
import com.adealink.weparty.R as APP_R

@RouterUri(
    path = [Playmate.SayWelcome.PATH],
    desc = "欢迎语"
)
class SayWelcomeActivity : BaseActivity() {

    private val binding by viewBinding(ActivitySayWelcomeBinding::inflate)
    private val pageViewModel by viewModels<WelcomePageViewModel>()
    private val welcomeViewModel by viewModels<WelcomeViewModel>()

    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
            }
        }
        inflateEmpty()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        pageViewModel.clickNewWelcomeLD.observeWithoutCache(this) {
            showWelcomeSettingDialog()
        }
        welcomeViewModel.welcomeListRltLD.observeWithoutCache(this) { rlt ->
            showFailedToast(rlt)
        }
        welcomeViewModel.welcomeListLD.observe(this) { list ->
            if (list.isEmpty()) {
                inflateEmpty()
            } else {
                inflateWelcomeList()
            }
        }
        onWelcomeAddLD.observeWithoutCache(this) {
            loadWelcomeInfo()
        }
        onWelcomeUpdateLD.observeWithoutCache(this) {
            loadWelcomeInfo()
        }
        onWelcomeDeleteLD.observeWithoutCache(this) {
            welcomeViewModel.onWelcomeDelete(it)
        }
    }

    override fun loadData() {
        super.loadData()
        loadWelcomeInfo()
    }

    private fun loadWelcomeInfo() {
        showLoading()
        welcomeViewModel.loadWelcomeInfo().observe(this) {
            dismissLoading()
        }
    }

    private fun showWelcomeSettingDialog() {
        val list = welcomeViewModel.welcomeListLD.value ?: return
        if (list.size >= MAX_WELCOME_COUNT) {
            showToast(
                getCompatString(R.string.playmate_max_welcome_count, MAX_WELCOME_COUNT.toString())
            )
            return
        }

        CommonTextActionDialog.Builder()
            .addItem(R.id.id_welcome_add_text, APP_R.string.common_text)
            .addItem(R.id.id_welcome_add_voice, APP_R.string.common_voice)
            .showCancel(false)
            .actionListener(object : CommonTextActionDialog.CommonTextActionListener {
                override fun onActionClick(viewId: Int) {
                    when (viewId) {
                        R.id.id_welcome_add_text -> {
                            Router.build(this@SayWelcomeActivity, Playmate.SayWelcomeSetting.PATH)
                                .putExtra(
                                    Playmate.SayWelcomeSetting.EXTRA_WELCOME_TYPE,
                                    PLAYMATE_WELCOME_TEXT
                                )
                                .start()
                        }

                        R.id.id_welcome_add_voice -> {
                            Router.build(this@SayWelcomeActivity, Playmate.SayWelcomeSetting.PATH)
                                .putExtra(
                                    Playmate.SayWelcomeSetting.EXTRA_WELCOME_TYPE,
                                    PLAYMATE_WELCOME_VOICE
                                )
                                .start()
                        }
                    }
                }
            }).build().show(supportFragmentManager)
    }

    private fun inflateEmpty() {
        var fragment =
            supportFragmentManager.findFragmentByTag(EmptyWelcomeFragment.TAG) as? EmptyWelcomeFragment
        if (fragment?.isAdded == true) {
            return
        }

        fragment = EmptyWelcomeFragment()
        supportFragmentManager.beginTransaction()
            .replace(R.id.fcv_container, fragment, EmptyWelcomeFragment.TAG)
            .commitAllowingStateLoss()
    }

    private fun inflateWelcomeList() {
        var fragment =
            supportFragmentManager.findFragmentByTag(WelcomeListFragment.TAG) as? WelcomeListFragment
        if (fragment?.isAdded == true) {
            return
        }

        fragment = WelcomeListFragment()
        supportFragmentManager.beginTransaction()
            .replace(R.id.fcv_container, fragment, WelcomeListFragment.TAG)
            .commitAllowingStateLoss()
    }

}