package com.gh.gamecenter.personalhome.border

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.core.utils.UrlFilterUtils
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.feature.entity.AvatarBorderEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

class ChooseAvatarBorderViewModel(application: Application, val categoryId: String) : AndroidViewModel(application) {
    val pendantsLiveData = MutableLiveData<ArrayList<AvatarBorderEntity>>()

    @SuppressLint("CheckResult")
    fun getPendants() {
        RetrofitManager.getInstance().api
            .getPendants(getFilter())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<ArrayList<AvatarBorderEntity>>() {
                override fun onSuccess(data: ArrayList<AvatarBorderEntity>) {
                    pendantsLiveData.postValue(data)
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    pendantsLiveData.postValue(null)
                }
            })
    }

    private fun getFilter(): String {
        return UrlFilterUtils.getFilterQuery("category", categoryId)

    }

    class Factory(
        private val mApplication: Application,
        private val mCategoryId: String
    ) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return ChooseAvatarBorderViewModel(mApplication, mCategoryId) as T
        }
    }
}