package com.adealink.weparty.room.create.viewmodel

import androidx.lifecycle.LiveData
import com.adealink.frame.base.CommonDataNullError
import com.adealink.frame.base.Rlt
import com.adealink.frame.base.fastLazy
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.module.image.data.PhotoData
import com.adealink.weparty.room.data.UpdateRoomInfoReq
import com.adealink.weparty.room.data.isAutoMuteMic
import com.adealink.weparty.room.datasource.remote.RoomHttpService
import com.adealink.weparty.room.sdk.service.roomService
import com.adealink.weparty.util.uploadPhoto
import io.trtc.tuikit.atomicxcore.api.live.LiveInfo
import kotlinx.coroutines.launch

class EditRoomViewModel : BaseViewModel() {

    private val roomHttpService by fastLazy {
        App.instance.networkService.getHttpService(RoomHttpService::class.java)
    }

    var roomCover: PhotoData? = null
    val roomCoverLD = ExtMutableLiveData<PhotoData?>()

    var roomName: String? = null
    val roomNameLD = ExtMutableLiveData<String?>()

    private var autoMuteMic: Boolean = true

    val autoMuteMicLD = ExtMutableLiveData<Boolean>()

    private var liveInfo: LiveInfo? = null

    fun setRoomInfo(liveInfo: LiveInfo?) {
        this.liveInfo = liveInfo
        updateRoomInfo(liveInfo)
    }

    private fun updateRoomInfo(roomInfo: LiveInfo?) {
        roomName = roomInfo?.liveName
        roomNameLD.send(roomName)

        roomCover = PhotoData(roomInfo?.coverURL, null, null)
        roomCoverLD.send(roomCover)

        this.autoMuteMic = liveInfo?.isAutoMuteMic() ?: false
        autoMuteMicLD.send(autoMuteMic)
    }

    fun changeCover(path: String?, uri: String?) {
        viewModelScope.launch {
            val data = PhotoData(null, path, uri)
            roomCover = data
            roomCoverLD.send(data)
        }
    }

    fun updateRoomName(name: String?) {
        this.roomName = name
        roomNameLD.send(name)
    }

    fun autoMuteMic(mute: Boolean) {
        autoMuteMic = mute
    }

    fun updateRoom(): LiveData<Rlt<Any>> {
        val liveData = OnceMutableLiveData<Rlt<Any>>()
        viewModelScope.launch {
            val roomId = roomService.joinController.getJoinedRoomId()
            if (roomId.isNullOrEmpty()) {
                liveData.send(Rlt.Failed(CommonDataNullError()))
                return@launch
            }

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

            val roomCover = roomCover
            if (roomCover == null || roomCover.isNull()) {
                liveData.send(Rlt.Failed(CommonDataNullError()))
                return@launch
            }

            if (liveInfo?.liveName == roomName
                && liveInfo?.coverURL == roomCover.url
                && liveInfo?.isAutoMuteMic() == autoMuteMic
            ) {
                //没有变化
                liveData.send(Rlt.Success(Any()))
                return@launch
            }

            when (val uploadRlt = uploadRoomCover(roomCover)) {
                is Rlt.Failed -> {
                    liveData.send(uploadRlt)
                    return@launch
                }

                is Rlt.Success -> {
                    //Ntd.
                }
            }

            val rlt = roomHttpService.updateRoomInfo(
                UpdateRoomInfoReq(
                    roomId, roomName, roomCover.url ?: "", autoMuteMic
                )
            )
            liveData.send(rlt)
        }
        return liveData
    }

    private suspend fun uploadRoomCover(cover: PhotoData): Rlt<PhotoData> {
        when (val rlt = uploadPhoto(cover)) {
            is Rlt.Failed -> {
                return rlt
            }

            is Rlt.Success -> {
                val photoData = rlt.data.firstOrNull()
                if (photoData == null || photoData.isNull()) {
                    return Rlt.Failed(CommonDataNullError())
                }
                return Rlt.Success(photoData)
            }
        }
    }

}