package com.sambath.klaklouk_main.screen.login

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.service.ApiService
import com.sambath.klaklouk_main.remote.LoginRequest
import com.sambath.klaklouk_main.util.Const
import com.sambath.klaklouk_main.util.ModelPreferencesManager
import com.sambath.klaklouk_main.util.PrefHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class LoginViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
    private val _state = MutableLiveData<LoginViewState>(LoginViewState())
    val state: LiveData<LoginViewState> = _state

    fun login(username: String, password: String) {
        if (_state.value!!.isProgress) return
        val request = LoginRequest(username, password)
        _state.value = prev().copy(isProgress = true)

        disposables.add(
            apiService.login(request)
                .timeout(10, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == 1) {
                        if (it.data?.user?.isSuspend == false) {
                            _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
                            getCfSet()
                            prefHelper.setUserToken(it.data.token ?: "")
                            prefHelper.setChannelType(1)
                            prefHelper.setMaxBetting(it.data.user.amountMax)
                            prefHelper.setMinBetting(it.data.user.amountMin)

                            it.data.user.let { it1 ->
                                ModelPreferencesManager.putUser(it1, Const.USER_KEY)
                                ModelPreferencesManager.putChannelList(
                                    it1.channelList,
                                    Const.CH_LIST_KEY
                                )
                            }
                        } else {
                            _state.value = prev().copy(
                                isProgress = false,
                                error = "Your account have been blocked"
                            )
                        }
                    } else {
                        _state.value =
                            prev().copy(isProgress = false, error = "[${it.message.description}]")
                    }
                }, {
                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
                    _state.value = prev().copy(isProgress = false, error = message)
                })
        )
    }

    private fun prev() = _state.value!!

    private fun getCfSet() {
        disposables.add(
            apiService.getBettingType()
                .timeout(60, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == "1") {
                        it.data.let { cSetList ->
                            ModelPreferencesManager.putCfList(
                                cSetList,
                                Const.CF_KEY
                            )
                        }
                    } else {
                        Log.d("CfCreateBetting", "api result : " + it.resultCode)
                    }
                }, {
                    Log.d("CfCreateBetting", it.getErrorCode())
                })
        )
    }


}