package com.adealink.weparty.playmate.saywelcome

import android.os.Bundle
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.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.frame.util.onClick
import com.adealink.weparty.commonui.BaseActivity
import com.adealink.weparty.commonui.toast.util.showToast
import com.adealink.weparty.commonui.widget.CommonDialog
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.module.playmate.data.SayWelcomeData
import com.adealink.weparty.module.playmate.data.SayWelcomeStatus
import com.adealink.weparty.playmate.R
import com.adealink.weparty.playmate.databinding.ActivitySayWelcomeBinding
import com.adealink.weparty.playmate.saywelcome.fragment.WelcomeAddTextFragment
import com.adealink.weparty.playmate.saywelcome.fragment.WelcomeAddVoiceFragment
import com.adealink.weparty.playmate.saywelcome.fragment.WelcomeUnderReviewFragment
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.SayWelcomeSetting.PATH],
    desc = "欢迎语设置"
)
class SayWelcomeSettingActivity : BaseActivity() {

    @BindExtra(Playmate.SayWelcomeSetting.EXTRA_WELCOME_TYPE)
    var welcomeType: Int? = null

    @BindExtra(Playmate.SayWelcomeSetting.EXTRA_WELCOME_DATA)
    var welcomeData: SayWelcomeData? = null

    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
            }
        }
        binding.topBar.backCallback = {
            finish()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        pageViewModel.afterSubmitLD.observeWithoutCache(this) {
            inflateUnderReviewWelcome()
        }
    }

    override fun loadData() {
        super.loadData()
        val type = welcomeType
        val data = welcomeData
        if (type == null && data == null) {
            finish()
            return
        }
        if (data != null) {
            //审核中
            if (SayWelcomeStatus.isUnderReview(data.status)) {
                inflateUnderReviewWelcome()
                return
            }
            if (data.isText()) {
                inflateAddTextWelcome(true)
            } else if (data.isVoice()) {
                inflateAddVoiceWelcome(true)
            }
        } else if (type != null) {
            if (type == PLAYMATE_WELCOME_TEXT) {
                inflateAddTextWelcome(false)
            } else if (type == PLAYMATE_WELCOME_VOICE) {
                inflateAddVoiceWelcome(false)
            }
        }
    }

    private fun inflateAddTextWelcome(showDelete: Boolean) {
        if (showDelete) {
            binding.topBar.setRightDrawable(R.drawable.playmate_welcome_delete_ic)
            binding.topBar.getRightView().onClick {
                showDeleteDialog()
            }
        } else {
            binding.topBar.setRightDrawable(0)
        }
        binding.topBar.setTitle(R.string.playmate_welcome_setting)
        var fragment =
            supportFragmentManager.findFragmentByTag(WelcomeAddTextFragment.TAG) as? WelcomeAddTextFragment
        if (fragment?.isAdded == true) {
            return
        }

        fragment = WelcomeAddTextFragment()
        fragment.arguments = Bundle().apply {
            putParcelable(Playmate.SayWelcomeSetting.EXTRA_WELCOME_DATA, welcomeData)
        }
        supportFragmentManager.beginTransaction()
            .replace(R.id.fcv_container, fragment, WelcomeAddTextFragment.TAG)
            .commitAllowingStateLoss()
    }

    private fun inflateAddVoiceWelcome(showDelete: Boolean) {
        if (showDelete) {
            binding.topBar.setRightDrawable(R.drawable.playmate_welcome_delete_ic)
            binding.topBar.getRightView().onClick {
                showDeleteDialog()
            }
        } else {
            binding.topBar.setRightDrawable(0)
        }
        binding.topBar.setTitle(R.string.playmate_welcome_setting)
        var fragment =
            supportFragmentManager.findFragmentByTag(WelcomeAddVoiceFragment.TAG) as? WelcomeAddVoiceFragment
        if (fragment?.isAdded == true) {
            return
        }

        fragment = WelcomeAddVoiceFragment()
        fragment.arguments = Bundle().apply {
            putParcelable(Playmate.SayWelcomeSetting.EXTRA_WELCOME_DATA, welcomeData)
        }
        supportFragmentManager.beginTransaction()
            .replace(R.id.fcv_container, fragment, WelcomeAddVoiceFragment.TAG)
            .commitAllowingStateLoss()
    }

    private fun inflateUnderReviewWelcome() {
        binding.topBar.setRightDrawable(0)
        binding.topBar.setTitle(APP_R.string.common_under_review)
        var fragment =
            supportFragmentManager.findFragmentByTag(WelcomeUnderReviewFragment.TAG) as? WelcomeUnderReviewFragment
        if (fragment?.isAdded == true) {
            return
        }

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

    private fun showDeleteDialog() {
        welcomeData ?: return
        CommonDialog.Builder()
            .message(getCompatString(APP_R.string.common_confirm_delete))
            .positiveText(getCompatString(APP_R.string.commonui_confirm))
            .setShowDefaultCancel(true)
            .onPositive {
                deleteWelcome()
            }
            .show(supportFragmentManager)
    }

    private fun deleteWelcome() {
        val data = welcomeData ?: return
        welcomeViewModel.delete(data.id).observe(this) { rlt ->
            dismissLoading()
            showToast(rlt)
            when (rlt) {
                is Rlt.Failed -> {
                    //Ntd.
                }

                is Rlt.Success -> {
                    finish()
                }
            }
        }
    }

}