package kr.co.vividnext.sodalive.mypage.can.status

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.mypage.can.CanRepository
import kr.co.vividnext.sodalive.mypage.can.status.charge.GetCanChargeStatusResponseItem
import kr.co.vividnext.sodalive.mypage.can.status.use.GetCanUseStatusResponseItem

class CanStatusViewModel(private val repository: CanRepository) : BaseViewModel() {
    private val _totalCanLiveData = MutableLiveData<Int>()
    val totalCanLiveData: LiveData<Int>
        get() = _totalCanLiveData

    private val _paidCanLiveData = MutableLiveData<Int>()
    val paidCanLiveData: LiveData<Int>
        get() = _paidCanLiveData

    private val _rewardCanLiveData = MutableLiveData<Int>()
    val rewardCanLiveData: LiveData<Int>
        get() = _rewardCanLiveData

    private val _canUseStatusLiveData = MutableLiveData<List<GetCanUseStatusResponseItem>>()
    val canUseStatusLiveData: LiveData<List<GetCanUseStatusResponseItem>>
        get() = _canUseStatusLiveData

    private val _canChargeStatusLiveData = MutableLiveData<List<GetCanChargeStatusResponseItem>>()
    val canChargeStatusLiveData: LiveData<List<GetCanChargeStatusResponseItem>>
        get() = _canChargeStatusLiveData

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

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

    private val pageSize = 20
    private var canUsesPage = 1

    fun getCanStatus() {
        _isLoading.value = true
        compositeDisposable.add(
            repository.getCanStatus("Bearer ${SharedPreferenceManager.token}")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    {
                        _isLoading.value = false
                        if (it.success && it.data != null) {
                            _totalCanLiveData.postValue(it.data.chargeCan + it.data.rewardCan)
                            _paidCanLiveData.postValue(it.data.chargeCan)
                            _rewardCanLiveData.postValue(it.data.rewardCan)
                        } 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 getCanUseStatus() {
        _isLoading.value = true
        compositeDisposable.add(
            repository.getCanUseStatus(
                canUsesPage,
                pageSize,
                "Bearer ${SharedPreferenceManager.token}"
            )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    {
                        _isLoading.value = false
                        if (it.success && it.data != null) {
                            _canUseStatusLiveData.postValue(it.data!!)
                        } 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 getCanChargeStatus() {
        _isLoading.value = true
        compositeDisposable.add(
            repository.getCanChargeStatus(
                canUsesPage,
                pageSize,
                "Bearer ${SharedPreferenceManager.token}"
            )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    {
                        _isLoading.value = false
                        if (it.success && it.data != null) {
                            _canChargeStatusLiveData.postValue(it.data!!)
                        } else {
                            if (it.message != null) {
                                _toastLiveData.postValue(it.message)
                            } else {
                                _toastLiveData.postValue(
                                    "알 수 없는 오류가 발생했습니다. 다시 시도해 주세요."
                                )
                            }
                        }
                    },
                    {
                        _isLoading.value = false
                        it.message?.let { message -> Logger.e(message) }
                        _toastLiveData.postValue("알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.")
                    }
                )
        )
    }
}
