package kr.co.vividnext.sodalive.settings.notification

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.user.UserRepository

class NotificationSettingsViewModel(
    private val userRepository: UserRepository
) : BaseViewModel() {
    private val _toastLiveData = MutableLiveData<String?>()
    val toastLiveData: LiveData<String?>
        get() = _toastLiveData

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

    private var _followingChannelLiveNotice = MutableLiveData(false)
    val followingChannelLiveNotice: LiveData<Boolean>
        get() = _followingChannelLiveNotice

    private var _isMessage = MutableLiveData(false)
    val isMessage: LiveData<Boolean>
        get() = _isMessage

    private var _followingChannelUploadContentNotice = MutableLiveData(false)
    val followingChannelUploadContentNotice: LiveData<Boolean>
        get() = _followingChannelUploadContentNotice

    fun getNotificationSettings() {
        _isLoading.value = true

        compositeDisposable.add(
            userRepository.getMemberInfo(token = "Bearer ${SharedPreferenceManager.token}")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    {
                        _isLoading.value = false

                        if (it.success && it.data != null) {
                            val data = it.data!!
                            _isMessage.value = data.messageNotice ?: false
                            _followingChannelUploadContentNotice.value =
                                data.followingChannelUploadContentNotice ?: false
                            _followingChannelLiveNotice.value =
                                data.followingChannelLiveNotice ?: false
                        } else {
                            if (it.message != null) {
                                _toastLiveData.postValue(it.message)
                            } else {
                                _toastLiveData.postValue(
                                    "알 수 없는 오류가 발생했습니다. 다시 시도해 주세요."
                                )
                            }
                        }
                    },
                    {
                        _isLoading.value = false
                        it.message?.let { message -> Logger.e(message) }
                        _toastLiveData.postValue(
                            "다시 시도해 주세요.\n계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
                        )
                    }
                )
        )
    }

    fun toggleFollowingChannelLiveNotice() {
        _followingChannelLiveNotice.value = !followingChannelLiveNotice.value!!
        updateNotificationSettings(live = followingChannelLiveNotice.value!!)
    }

    fun toggleFollowingChannelUploadContentNotice() {
        _followingChannelUploadContentNotice.value = !followingChannelUploadContentNotice.value!!
        updateNotificationSettings(uploadContent = followingChannelUploadContentNotice.value!!)
    }

    fun toggleMessage() {
        _isMessage.value = !isMessage.value!!
        updateNotificationSettings(message = isMessage.value!!)
    }

    private fun updateNotificationSettings(
        live: Boolean? = null,
        uploadContent: Boolean? = null,
        message: Boolean? = null
    ) {
        if (live != null || uploadContent != null || message != null) {
            compositeDisposable.add(
                userRepository.updateNotificationSettings(
                    request = UpdateNotificationSettingRequest(
                        live,
                        uploadContent,
                        message
                    ),
                    token = "Bearer ${SharedPreferenceManager.token}"
                )
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe({}, {})
            )
        }
    }
}
