package com.adealink.weparty.profile.edit.dialog

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.viewModels
import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.naviBarHeight
import com.adealink.weparty.commonui.widget.BottomDialogFragment
import com.adealink.weparty.module.profile.data.UserVoiceEditData
import com.adealink.weparty.module.profile.data.VoiceData
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.DialogEditTalentVoiceBinding
import com.adealink.weparty.profile.edit.dialog.fragment.VoiceEditFragment
import com.adealink.weparty.profile.edit.dialog.fragment.VoiceFragment
import com.adealink.weparty.profile.edit.dialog.fragment.VoiceUnderReviewFragment
import com.adealink.weparty.profile.edit.dialog.viewmodel.EditVoicePage
import com.adealink.weparty.profile.edit.dialog.viewmodel.EditVoiceViewModel

class EditTalentVoiceDialog : BottomDialogFragment(R.layout.dialog_edit_talent_voice) {

    companion object {
        private const val TAG_VOICE = "TAG_TALENT_VOICE"
        private const val TAG_VOICE_EDIT = "TAG_TALENT_VOICE_EDIT"

        private const val TAG_VOICE_REVIEW = "TAG_TALENT_VOICE_REVIEW"
    }

    private val binding by viewBinding(DialogEditTalentVoiceBinding::bind)

    private val viewModel by viewModels<EditVoiceViewModel>()
    private var callback: ITalentVoiceCallback? = null

    private var editVoice: UserVoiceEditData? = null
    private var voice: VoiceData? = null

    fun setVoice(voice: UserVoiceEditData?) {
        this.editVoice = voice
        this.voice = VoiceData(url = voice?.voiceBar, null, null)
    }

    fun setCallback(callback: ITalentVoiceCallback?) {
        this.callback = callback
    }

    override fun initViews() {
        super.initViews()
    }

    override fun observeViewModel() {
        super.observeViewModel()
        binding.flContent.updateLayoutParams<ConstraintLayout.LayoutParams> {
            bottomMargin = activity?.naviBarHeight() ?: 0
        }
        viewModel.pageLD.observeWithoutCache(viewLifecycleOwner) { page ->
            when (page) {
                EditVoicePage.VOICE -> {
                    inflateVoiceFragment()
                }

                EditVoicePage.EDIT -> {
                    inflateVoiceEditFragment()
                }

                EditVoicePage.REVIEW -> {
                    inflateUnderReviewFragment()
                }
            }
        }
    }

    override fun loadData() {
        super.loadData()
        viewModel.setEditVoice(editVoice)
    }

    private fun inflateVoiceFragment() {
        val fragment = childFragmentManager.findFragmentByTag(TAG_VOICE)
        if (fragment?.isAdded == true) {
            return
        }
        childFragmentManager.beginTransaction()
            .replace(
                R.id.fl_content,
                VoiceFragment(),
                TAG_VOICE
            )
            .commitAllowingStateLoss()
    }

    private fun inflateVoiceEditFragment() {
        val fragment = childFragmentManager.findFragmentByTag(TAG_VOICE_EDIT)
        if (fragment?.isAdded == true) {
            return
        }
        childFragmentManager.beginTransaction()
            .replace(
                R.id.fl_content,
                VoiceEditFragment(),
                TAG_VOICE_EDIT
            )
            .commitAllowingStateLoss()
    }

    private fun inflateUnderReviewFragment() {
        val fragment = childFragmentManager.findFragmentByTag(TAG_VOICE_REVIEW)
        if (fragment?.isAdded == true) {
            return
        }
        childFragmentManager.beginTransaction()
            .replace(
                R.id.fl_content,
                VoiceUnderReviewFragment(),
                TAG_VOICE_REVIEW
            )
            .commitAllowingStateLoss()
    }


    interface ITalentVoiceCallback {
        fun onEditVoice(voice: VoiceData?)
    }

}