package kr.co.vividnext.sodalive.audio_content.series.detail

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.audio_content.series.SeriesRepository
import kr.co.vividnext.sodalive.base.BaseViewModel
import kr.co.vividnext.sodalive.common.SharedPreferenceManager
import kr.co.vividnext.sodalive.user.UserRepository

class SeriesDetailViewModel(
    private val repository: SeriesRepository,
    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 _seriesDetailLiveData = MutableLiveData<GetSeriesDetailResponse>()
    val seriesDetailLiveData: LiveData<GetSeriesDetailResponse>
        get() = _seriesDetailLiveData

    var seriesId = 0L

    lateinit var seriesDetailResponse: GetSeriesDetailResponse

    fun getSeriesDetail() {
        _isLoading.value = true

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

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