package com.sambath.sbc.screen.login

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.sambath.sbc.app.getErrorCode
import com.sambath.sbc.base.BaseViewModel
import com.sambath.sbc.remote.service.ApiService
import com.sambath.sbc.remote.LoginRequest
import com.sambath.sbc.util.Const
import com.sambath.sbc.util.ModelPreferencesManager
import com.sambath.sbc.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({
                    Log.d("login", it.toString())
                    if (it.resultCode == "1") {
                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
                        prefHelper.setUserToken(it.data?.token ?: "")
                        prefHelper.setChannelType(1)
                        ModelPreferencesManager.put(it.data?.user, Const.USER_KEY)
                    } else {
                        _state.value =
                            prev().copy(isProgress = false, error = "[${it.message.description}]")
                    }
                }, {
                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
//                    if (it is AppException && it.isEmpty) {
//                        _state.value =
//                            _state.value?.copy(isProgress = false, data = empty(), isEmpty = true)
//                    } else {
//                        val message: String =
//                            if (it is AppException)
//                                "មិនអាចទាញយកទិន្នន័យបាន\nError[${it.message}]"
//                            else if (it is ConnectException || it is HttpException)
//                                "មិនអាចភ្ជាប់ទៅប្រព័ន្ធបាន\nError[Client Connection]"
//                            else if (it is TimeoutException)
//                                "មិនអាចភ្ជាប់ទៅប្រព័ន្ធបាន\nError[Timeout]"
//                            else
//                                "មិនអាចភ្ជាប់ទៅប្រព័ន្ធបាន\nError[Client]"
//                        _state.value =
//                            _state.value?.copy(isProgress = false, error = message, data = null)
//                    }
                    _state.value = prev().copy(isProgress = false, error = message)
                })
        )
    }

    private fun prev() = _state.value!!


}