package kr.co.vividnext.sodalive.audio_content.main.curation

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.AudioContentRepository
import kr.co.vividnext.sodalive.audio_content.main.GetAudioContentCurationResponse
import kr.co.vividnext.sodalive.base.BaseViewModel
import kr.co.vividnext.sodalive.common.SharedPreferenceManager

class AudioContentMainCurationViewModel(
    private val repository: AudioContentRepository
) : 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 _curationListLiveData = MutableLiveData<List<GetAudioContentCurationResponse>>()
    val curationListLiveData: LiveData<List<GetAudioContentCurationResponse>>
        get() = _curationListLiveData

    var page = 1
    var isLast = false
    private val pageSize = 10

    fun getCurationList() {
        if (!_isLoading.value!! && !isLast) {
            _isLoading.value = true
            compositeDisposable.add(
                repository.getCurationList(
                    page = page,
                    size = pageSize,
                    token = "Bearer ${SharedPreferenceManager.token}"
                )
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                        {
                            if (it.success && it.data != null) {
                                page += 1

                                if (it.data.isNotEmpty()) {
                                    _curationListLiveData.postValue(it.data!!)
                                } else {
                                    _curationListLiveData.postValue(listOf())
                                    isLast = true
                                }
                            } else {
                                if (it.message != null) {
                                    _toastLiveData.postValue(it.message)
                                } else {
                                    _toastLiveData.postValue(
                                        "큐레이션을 불러오지 못했습니다. 다시 시도해 주세요.\n" +
                                            "계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
                                    )
                                }
                            }

                            _isLoading.value = false
                        },
                        {
                            _isLoading.value = false
                            it.message?.let { message -> Logger.e(message) }
                            _toastLiveData.postValue(
                                "큐레이션을 불러오지 못했습니다. 다시 시도해 주세요.\n" +
                                    "계속 같은 문제가 발생할 경우 고객센터로 문의 주시기 바랍니다."
                            )
                        }
                    )
            )
        }
    }

    fun refresh() {
        page = 1
        isLast = false
        getCurationList()
    }
}
