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

import com.adealink.frame.mvvm.view.viewBinding
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.BaseFragment
import com.adealink.weparty.profile.R
import com.adealink.weparty.profile.databinding.FragmentTalentVoiceBinding
import com.adealink.weparty.profile.edit.dialog.EditTalentVoiceDialog
import com.adealink.weparty.profile.edit.dialog.viewmodel.EditVoicePage
import com.adealink.weparty.profile.edit.dialog.viewmodel.EditVoiceViewModel
import com.adealink.weparty.viewmodel.parentFragmentViewModels

class VoiceFragment : BaseFragment(R.layout.fragment_talent_voice) {

    private val binding by viewBinding(FragmentTalentVoiceBinding::bind)

    private val viewModel by parentFragmentViewModels<EditVoiceViewModel, EditTalentVoiceDialog>(
        EditTalentVoiceDialog::class.java
    )

    override fun initViews() {
        super.initViews()
        val voiceData = viewModel.voiceDataLD.value
        binding.vSound.setSound(voiceData)
        binding.btnRecord.onClick {
            clearAndRecode()
        }
    }

    override fun observeViewModel() {
        super.observeViewModel()
        viewModel.voiceDataLD.observeWithoutCache(viewLifecycleOwner) {
            binding.vSound.setSound(it)
        }
    }

    private fun clearAndRecode() {
        viewModel.clearVoice()
        viewModel.setVoiceData(null)
        viewModel.showPage(EditVoicePage.EDIT)
    }

}