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.mvvm.livedata.ExtMutableLiveData
import com.adealink.frame.mvvm.livedata.OnceMutableLiveData
import com.adealink.frame.mvvm.viewmodel.BaseViewModel
import com.adealink.weparty.image.ROOM_IMAGE_MAX_HEIGHT
import com.adealink.weparty.image.ROOM_IMAGE_MAX_SIZE_KB
import com.adealink.weparty.image.ROOM_IMAGE_MAX_WIDTH
import com.adealink.weparty.image.ROOM_IMAGE_MIN_QUALITY
import com.adealink.weparty.module.image.data.PhotoData
import com.adealink.weparty.module.room.data.MyRoomInfo
import com.adealink.weparty.room.data.CreateRoomReq
import com.adealink.weparty.room.manager.roomManager
import com.adealink.weparty.util.uploadPhoto
import kotlinx.coroutines.launch

class CreateRoomViewModel : BaseViewModel() {

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

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

    private var forbidAudio: Boolean = true

    val forbidAudioLD = ExtMutableLiveData<Boolean>()

    init {
        val roomInfo = roomManager.getLocalMyRoomInfo()
        updateRoomInfo(roomInfo)
    }

    fun loadMyRoomInfo() {
        viewModelScope.launch {
            when (val rlt = roomManager.getMyRoomInfo()) {
                is Rlt.Failed -> {
                    //Ntd.
                }

                is Rlt.Success -> {
                    updateRoomInfo(rlt.data)
                }
            }
        }
    }

    private fun updateRoomInfo(roomInfo: MyRoomInfo?) {
        roomName = roomInfo?.roomTitle
        roomNameLD.send(roomName)

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

        forbidAudio = roomInfo?.forbidAudio ?: false
        forbidAudioLD.send(forbidAudio)
    }

    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 micMute(mute: Boolean) {
        forbidAudio = mute

    }

    fun createRoom(): LiveData<Rlt<String>> {
        val liveData = OnceMutableLiveData<Rlt<String>>()
        viewModelScope.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
            }
            when (val uploadRlt = uploadRoomCover(roomCover)) {
                is Rlt.Failed -> {
                    liveData.send(uploadRlt)
                    return@launch
                }

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

            val rlt = roomManager.createRoom(
                CreateRoomReq(
                    roomName,
                    roomCover.url ?: "",
                    forbidAudio
                )
            )
            liveData.send(rlt)
        }
        return liveData
    }

    private suspend fun uploadRoomCover(cover: PhotoData): Rlt<PhotoData> {
        when (val rlt = uploadPhoto(
            cover,
            ROOM_IMAGE_MAX_WIDTH,
            ROOM_IMAGE_MAX_HEIGHT,
            ROOM_IMAGE_MAX_SIZE_KB,
            ROOM_IMAGE_MIN_QUALITY
        )) {
            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)
            }
        }
    }

}