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

import androidx.lifecycle.LiveData
import com.adealink.frame.base.CommonDataNullError
import com.adealink.frame.base.Rlt
import com.adealink.frame.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.App
import com.adealink.weparty.commonui.ext.isFailure
import com.adealink.weparty.module.profile.data.UserVoiceEditData
import com.adealink.weparty.module.profile.data.VoiceData
import com.adealink.weparty.module.profile.data.getDuration
import com.adealink.weparty.profile.data.UserVoiceApplyReq
import com.adealink.weparty.profile.datasource.remote.ProfileHttpService
import com.adealink.weparty.util.uploadVoice
import kotlinx.coroutines.launch

enum class EditVoicePage {
    VOICE, //展示当前语音
    EDIT, //编辑语音
    REVIEW, //审核
}

class EditVoiceViewModel : BaseViewModel() {

    private val profileHttpService by lazy {
        App.instance.networkService.getHttpService(ProfileHttpService::class.java)
    }

    val pageLD = ExtMutableLiveData<EditVoicePage>()

    fun showPage(page: EditVoicePage) {
        pageLD.send(page)
    }

    private var editVoice: UserVoiceEditData? = null
    val editVoiceLD = ExtMutableLiveData<UserVoiceEditData?>()
    fun setEditVoice(editVoice: UserVoiceEditData?) {
        this.editVoice = editVoice
        editVoiceLD.send(editVoice)
        setVoiceData(editVoice?.voiceBar)
        if (editVoice == null) {
            showPage(EditVoicePage.EDIT)
        } else if (editVoice.isDisplay()) {
            showPage(EditVoicePage.VOICE)
        } else if (editVoice.isUnderReview()) {
            showPage(EditVoicePage.REVIEW)
        } else {
            showPage(EditVoicePage.EDIT)
        }
    }


    fun clearVoice(): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val rlt = profileHttpService.clearUserVoice()
            liveData.send(rlt)
        }
        return liveData
    }

    /**
     * 录制声音
     */
    private var voiceData: VoiceData? = null
    val voiceDataLD = ExtMutableLiveData<VoiceData>()

    fun setVoiceData(url: String?) {
        val data = VoiceData(url = url, null, null)
        voiceData = data
        voiceDataLD.send(data)
    }

    fun setVoiceData(path: String?, duration: Long?) {
        val data = VoiceData(url = null, path = path, duration = duration)
        voiceData = data
        voiceDataLD.send(data)
    }

    fun submit(): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val voiceData = voiceData
            if (voiceData == null || voiceData.isNull()) {
                liveData.send(Rlt.Failed(CommonDataNullError()))
                return@launch
            }

            val uploadRlt = uploadVoice(voiceData)
            if (uploadRlt.isFailure) {
                liveData.send(uploadRlt)
                return@launch
            }

            val voiceUrl = voiceData.url
            if (voiceUrl.isNullOrEmpty()) {
                liveData.send(Rlt.Failed(CommonDataNullError()))
                return@launch
            }

            val applyRlt = profileHttpService.userVoiceApply(
                UserVoiceApplyReq(
                    voiceUrl, voiceData.getDuration()
                )
            )
            liveData.send(applyRlt)
        }
        return liveData
    }

}