package com.sambath.kunkhmer.screen.event

import android.graphics.Bitmap
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.CreateEventRequest
import com.sambath.kunkhmer.remote.EventData
import com.sambath.kunkhmer.remote.EventInfo
import com.sambath.kunkhmer.remote.MatchesData
import com.sambath.kunkhmer.remote.service.ApiService
import com.sambath.kunkhmer.util.PrefHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

class EventViewModel(val apiService: ApiService, val prefHelper: PrefHelper) : BaseViewModel() {
    private val _state = MutableLiveData<EventViewState>(EventViewState())
    val state: LiveData<EventViewState> = _state
    private fun prev() = _state.value!!

    data class EventViewState(
        val initial: Boolean = false,
        val isProgress: Boolean = false,
        val isLoginSuccess: Boolean = false,
        val error: String? = null,
        val eventsData: MutableList<EventInfo>? = null,
    )

    fun getEvents(){
        if (_state.value!!.isProgress) return
        _state.value = prev().copy(isProgress = true)

        disposables.add(
            apiService.getEvents()
                .timeout(10, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == 1) {
                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true, eventsData = it.data!!.obj)
                    } else {
                        _state.value =
                            prev().copy(isProgress = false, error = "[${it.message.description}]")
                    }
                }, {
                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
                    _state.value = prev().copy(isProgress = false, error = message)
                })
        )
    }

    fun createEvent(title: String, desc: String, matchFighterId: String){
        if (_state.value!!.isProgress) return
        _state.value = prev().copy(isProgress = true)
        val request = CreateEventRequest(title, desc, matchFighterId)

        disposables.add(
            apiService.createEvent(request)
                .timeout(10, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({
                    if (it.resultCode == 1) {
                        _state.value = prev().copy(isProgress = false, isLoginSuccess = true)
                    } else {
                        _state.value =
                            prev().copy(isProgress = false, error = "[${it.message.description}]")
                    }
                }, {
                    val message: String = "ប្រតិបត្តិការមិនជោគជ័យ " + it.getErrorCode()
                    _state.value = prev().copy(isProgress = false, error = message)
                })
        )
    }
}