package com.gh.gamecenter.feedback.view.help

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.common.mvvm.Resource
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.feedback.HaloApp
import com.gh.gamecenter.feedback.entity.HelpCategoryEntity
import com.gh.gamecenter.feedback.retrofit.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException

class HelpQaCategoryViewModel(application: Application, val qaCollectionId: String? = "") :
    AndroidViewModel(application) {

    private val mApi = RetrofitManager.getInstance().api
    val categoryLiveData = MutableLiveData<Resource<List<HelpCategoryEntity>>>()

    init {
        initData()
    }

    fun initData() {
        if (!qaCollectionId.isNullOrEmpty()) {
            getQaCollection()
        } else {
            getHelpCategory()
        }
    }

    @SuppressLint("CheckResult")
    fun getQaCollection() {
        mApi.getQaCollection(qaCollectionId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<List<HelpCategoryEntity>>() {
                override fun onSuccess(data: List<HelpCategoryEntity>) {
                    categoryLiveData.postValue(Resource.success(data))
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    val httpException = if (exception is HttpException) {
                        exception
                    } else null
                    categoryLiveData.postValue(Resource.error(httpException))
                }
            })
    }

    @SuppressLint("CheckResult")
    fun getHelpCategory() {
        mApi.getHelpCategory()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : BiResponse<List<HelpCategoryEntity>>() {
                override fun onSuccess(data: List<HelpCategoryEntity>) {
                    categoryLiveData.postValue(Resource.success(data))
                }

                override fun onFailure(exception: Exception) {
                    super.onFailure(exception)
                    val httpException = if (exception is HttpException) {
                        exception
                    } else null
                    categoryLiveData.postValue(Resource.error(httpException))
                }
            })
    }


    class Factory(private val mQaCollectionId: String?) : ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return HelpQaCategoryViewModel(HaloApp.getInstance(), mQaCollectionId) as T
        }
    }
}