package com.sambath.kunkhmer.screen.main

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.sambath.kunkhmer.app.getErrorCode
import com.sambath.kunkhmer.base.BaseViewModel
import com.sambath.kunkhmer.remote.service.ApiService
import com.sambath.kunkhmer.screen.main.cflive.result.CfResultViewState
import com.sambath.kunkhmer.util.Const
import com.sambath.kunkhmer.util.ModelPreferencesManager
import com.sambath.kunkhmer.util.PrefHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class MainViewModel(val apiService: ApiService, val prefHelper: PrefHelper): BaseViewModel() {
    private val _stateChannel = MutableLiveData(MainDataViewState())
    val stateChannel: LiveData<MainDataViewState> = _stateChannel
    fun getChannel(){
        if (_stateChannel.value!!.isUserPro) return
        _stateChannel.value = prevChannel().copy(isUserPro = true)
        disposables.add(
            apiService.getChannel()
                .timeout(60, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == "1") {
                        _stateChannel.value = prevChannel().copy(
                            isUserPro = false,
                            errorUser = null,
                            channelInfo = it.data,
                            isChSuccess = true
                        )
                    } else {
                        _stateChannel.value =
                            prevChannel().copy(isUserPro = false, errorUser = "[${it.message.description}]")
                    }
                }, {
                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ\n" + it.getErrorCode()
                    _stateChannel.value = prevChannel().copy(isUserPro = false, errorUser = message)
                })
        )
    }
    private fun prevChannel() = _stateChannel.value!!

    private val _stateUser = MutableLiveData(MainDataViewState())
    val stateUser: LiveData<MainDataViewState> = _stateUser
    fun getUser(){
        if (_stateUser.value!!.isUserPro) return
        _stateUser.value = prevUser().copy(isUserPro = true)
        disposables.add(
            apiService.getUser()
                .timeout(60, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == "1") {
                        ModelPreferencesManager.put(it.data, Const.USER_KEY)
                        _stateUser.value = prevUser().copy(
                            isUserPro = false,
                            errorUser = null,
                            userInfo = it.data,
                            isUserSuccess = true
                        )
                    } else {
                        _stateUser.value =
                            prevChannel().copy(isUserPro = false, errorUser = "[${it.message.description}]")
                    }
                }, {
//                    Log.d("dataxzsd", it.getErrorCode())
                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ\n" + it.getErrorCode()
                    _stateUser.value = prevUser().copy(isUserPro = false, errorUser = message)
                })
        )
    }
    private fun prevUser() = _stateUser.value!!

    private val _state = MutableLiveData<CfResultViewState>(CfResultViewState())
    val state: LiveData<CfResultViewState> = _state
    fun getCfResuleData(){
        if (_state.value!!.isProgress) return
        _state.value = prev().copy(isProgress = true)
        disposables.add(
            apiService.getResult()
                .timeout(60, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == "1") {
//                        Log.d("dataxzsd", it.data.toString())
                        _state.value = prev().copy(
                            isProgress = false,
                            error = null,
                            isSuccess = true,
                            resultList = it.data?.results,
                            resultSummary = it.data?.resultSummary
                        )
                    } else {
                        _state.value =
                            prev().copy(
                                isProgress = false,
                                isSuccess = false,
                                error = "[${it.message.description}]"
                            )
                    }
                }, {
//                    Log.d("dataxzsd", it.getErrorCode())
                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ\n" + it.getErrorCode()
                    _state.value = prev().copy(
                        isProgress = false,
                        isSuccess = false,
                        error = message
                    )
                })
        )
    }

    private fun prev() = _state.value!!

}