package com.sambath.klaklouk_main.screen.main.cflive.betting

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.sambath.klaklouk_main.app.getErrorCode
import com.sambath.klaklouk_main.base.BaseViewModel
import com.sambath.klaklouk_main.remote.CreateBetting
import com.sambath.klaklouk_main.remote.service.ApiService
import com.sambath.klaklouk_main.util.PrefHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class CfBetViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
    private val _state = MutableLiveData<CfBetViewState>(CfBetViewState())
    val state: LiveData<CfBetViewState> = _state
    fun createBetting(createBetting: CreateBetting) {
        if (_state.value!!.isProgress) return
        _state.value = prev().copy(isProgress = true)
        disposables.add(
            apiService.createBetting(createBetting)
                .timeout(60, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == "1") {
                        Log.d("CfCreateBetting", "api result sss : " + it.data)
                        _state.value = prev().copy(
                            isProgress = false,
                            error = null,
                            isSuccess = true,
                            data = it.data
                        )
                    } else {
                        Log.d("CfCreateBetting", "api result nos: " + it.resultCode)
                        _state.value =
                            prev().copy(
                                isProgress = false,
                                isSuccess = false,
                                error = "[${it.message.description}]"
                            )
                    }
                }, {
                    Log.d("CfCreateBetting", "errro " + it.getErrorCode())
                    _state.value = prev().copy(
                        isProgress = false,
                        isSuccess = false,
                        error = "ប្រតិបត្តិការមិនជោគជ័យទេ!"
                    )
                })
        )
    }

    private fun prev() = _state.value!!

    private val _stateMaster = MutableLiveData<BettingTypeMasterState>(BettingTypeMasterState())
    val stateMaster: LiveData<BettingTypeMasterState> = _stateMaster

    fun getMaster() {
        if (_stateMaster.value!!.isProgress) return
        _stateMaster.value = prevMaster().copy(isProgress = true)
        disposables.add(
            apiService.getBettingType()
                .timeout(60, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == "1") {
                        _stateMaster.value = prevMaster().copy(
                            isProgress = false,
                            error = null,
                            isSuccess = true,
                            bettingTypeData = it.data
                        )
                    } else {
                        _stateMaster.value =
                            prevMaster().copy(
                                isProgress = false,
                                isSuccess = false,
                                error = "[${it.message.description}]"
                            )
                    }
                }, {
                    _stateMaster.value = prevMaster().copy(
                        isProgress = false,
                        isSuccess = false,
                        error = "ប្រតិបត្តិការមិនជោគជ័យទេ!"
                    )
                })
        )
    }

    private fun prevMaster() = _stateMaster.value!!

    private val _stateBtn = MutableLiveData<BtnMasterState>(BtnMasterState())
    val stateBtn: LiveData<BtnMasterState> = _stateBtn
    fun getBtnMaster() {
        if (_stateBtn.value!!.isProgress) return
        _stateBtn.value = prevBtn().copy(isProgress = true)
        disposables.add(
            apiService.getButton()
                .timeout(60, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == "1") {
                        Log.d("CfCreateBetting", "api result : " + it.data)
                        _stateBtn.value = prevBtn().copy(
                            isProgress = false,
                            error = null,
                            isSuccess = true,
                            btnData = it.data
                        )
                    } else {
                        Log.d("CfCreateBetting", "api result : " + it.resultCode)
                        _stateBtn.value =
                            prevBtn().copy(
                                isProgress = false,
                                isSuccess = false,
                                error = "[${it.message.description}]"
                            )
                    }
                }, {
                    Log.d("CfCreateBetting", "error")
                    _stateBtn.value = prevBtn().copy(
                        isProgress = false,
                        isSuccess = false,
                        error = "ប្រតិបត្តិការមិនជោគជ័យទេ!"
                    )
                })
        )
    }

    private fun prevBtn() = _stateBtn.value!!

    private val _stateWeight = MutableLiveData(WeightViewState())
    val stateWeight: LiveData<WeightViewState> = _stateWeight
    fun getWeight() {
        if (_stateWeight.value!!.isPro) return
        _stateWeight.value = prevWeight().copy(isPro = true)
        disposables.add(
            apiService.getWeight()
                .timeout(60, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == "1") {
                        _stateWeight.value = prevWeight().copy(
                            isPro = false,
                            error = null,
                            weightData = it.data,
                            isSuccess = true
                        )
                    } else {
                        _stateWeight.value =
                            prevWeight().copy(isPro = false, error = "[${it.message.description}]")
                    }
                }, {
//                    Log.d("dataxzsd", it.getErrorCode())
                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ\n" + it.getErrorCode()
                    _stateWeight.value = prevWeight().copy(isPro = false, error = message)
                })
        )
    }

    private fun prevWeight() = _stateWeight.value!!
}