package kr.co.vividnext.sodalive.live.room.donation

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.orhanobut.logger.Logger
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.schedulers.Schedulers
import kr.co.vividnext.sodalive.base.BaseViewModel
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.live.LiveRepository

class LiveRoomDonationMessageViewModel(private val repository: LiveRepository) : BaseViewModel() {
    private val _donationMessageListLiveData = MutableLiveData<List<LiveRoomDonationMessage>>()
    val donationMessageListLiveData: LiveData<List<LiveRoomDonationMessage>>
        get() = _donationMessageListLiveData

    private val _donationMessageCountLiveData = MutableLiveData(0)
    val donationMessageCountLiveData: LiveData<Int>
        get() = _donationMessageCountLiveData

    private val _toastLiveData = MutableLiveData<String?>()
    val toastLiveData: LiveData<String?>
        get() = _toastLiveData

    private var _isLoading = MutableLiveData(false)
    val isLoading: LiveData<Boolean>
        get() = _isLoading

    fun getDonationMessageList(roomId: Long) {
        _isLoading.value = true
        compositeDisposable.add(
            repository.getDonationMessageList(
                roomId = roomId,
                token = "Bearer ${SharedPreferenceManager.token}"
            )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    {
                        _isLoading.value = false
                        if (it.success && it.data != null) {
                            _donationMessageListLiveData.postValue(it.data!!)
                            _donationMessageCountLiveData.postValue(it.data!!.size)
                        } else {
                            if (it.message != null) {
                                _toastLiveData.postValue(it.message)
                            } else {
                                _toastLiveData.postValue(
                                    "알 수 없는 오류가 발생했습니다. 다시 시도해 주세요."
                                )
                            }
                        }
                    },
                    {
                        _isLoading.value = false
                        it.message?.let { message -> Logger.e(message) }
                        _toastLiveData.postValue("알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.")
                    }
                )
        )
    }

    fun deleteDonationMessage(roomId: Long, uuid: String) {
        _isLoading.value = true
        compositeDisposable.add(
            repository.deleteDonationMessage(
                roomId = roomId,
                uuid = uuid,
                token = "Bearer ${SharedPreferenceManager.token}"
            )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    {
                        _isLoading.value = false
                        if (it.success) {
                            getDonationMessageList(roomId)
                        } else {
                            if (it.message != null) {
                                _toastLiveData.postValue(it.message)
                            } else {
                                _toastLiveData.postValue(
                                    "알 수 없는 오류가 발생했습니다. 다시 시도해 주세요."
                                )
                            }
                        }
                    },
                    {
                        _isLoading.value = false
                        it.message?.let { message -> Logger.e(message) }
                        _toastLiveData.postValue("알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.")
                    }
                )
        )
    }
}
