package com.gh.gamecenter.video.label

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.entity.ActivityLabelEntity
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException

class VideoLabelViewModel(application: Application, val location: String, val bbsId: String) :
    AndroidViewModel(application) {

    val activityTags = MutableLiveData<ArrayList<ActivityLabelEntity>?>()

    init {
        getActivityTags()
    }

    private fun getActivityTags() {
        RetrofitManager.getInstance().api
            .getActivityTags(location, bbsId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<ArrayList<ActivityLabelEntity>>() {
                override fun onResponse(response: ArrayList<ActivityLabelEntity>?) {
                    super.onResponse(response)
                    activityTags.postValue(response)
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    activityTags.postValue(null)
                }
            })
    }

    class Factory(private val location: String, private val bbsId: String) : ViewModelProvider.NewInstanceFactory() {

        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return VideoLabelViewModel(HaloApp.getInstance().application, location, bbsId) as T
        }
    }
}