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

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.charge.pg.VerifyRequest

class CanPaymentTempViewModel(private val repository: CanPaymentTempRepository) : BaseViewModel() {
    private val _paymentMethodLiveData = MutableLiveData<CanPaymentTempActivity.PaymentMethod?>()
    val paymentMethodLiveData: LiveData<CanPaymentTempActivity.PaymentMethod?>
        get() = _paymentMethodLiveData

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

    fun chargeCan(
        can: Int,
        paymentGateway: PaymentGateway,
        onSuccess: (Long) -> Unit,
        onFailure: (String) -> Unit
    ) {
        _isLoading.value = true
        val request = ChargeTempRequest(can, can * 110, paymentGateway)
        compositeDisposable.add(
            repository.chargeCan(request, "Bearer ${SharedPreferenceManager.token}")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                    {
                        _isLoading.value = false
                        if (it.success && it.data != null) {
                            onSuccess(it.data.chargeId)
                        } else {
                            if (it.message != null) {
                                onFailure(it.message)
                            } else {
                                onFailure("알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.")
                            }
                        }
                    },
                    {
                        _isLoading.value = false
                        it.message?.let { message -> Logger.e(message) }
                        onFailure("알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.")
                    }
                )
        )
    }

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

    fun setPaymentMethod(paymentMethod: CanPaymentTempActivity.PaymentMethod) {
        _paymentMethodLiveData.value = paymentMethod
    }
}
