package com.gh.gamecenter.game.columncollection.detail

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.entity.GameColumnCollection
import com.gh.gamecenter.common.entity.LinkEntity
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import retrofit2.HttpException


class ColumnCollectionDetailViewModel(
    application: Application,
    private val mCollectionId: String
) : ListViewModel<LinkEntity, LinkEntity>(application) {

    private val mSensitiveApi = RetrofitManager.getInstance().api

    val collectionId: String
        get() = mCollectionId

    val columnCollection = MutableLiveData<GameColumnCollection>()

    fun rowCount() = if ((columnCollection.value?.style == "1-2")) 2 else 1

    override fun provideDataObservable(page: Int): Observable<MutableList<LinkEntity>> {
        return mSensitiveApi.getGameColumnCollectionList(mCollectionId, page)
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { mResultLiveData.postValue(it) }
    }

    @SuppressLint("CheckResult")
    fun getGameColumnCollection() {
        mSensitiveApi.getGameColumnCollection(mCollectionId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Response<GameColumnCollection>() {
                override fun onResponse(response: GameColumnCollection?) {
                    super.onResponse(response)
                    columnCollection.postValue(response)
                    loadData()
                }

                override fun onFailure(e: HttpException?) {
                    super.onFailure(e)
                    if (e != null && e.code() == 404) {
                        mLoadStatusLiveData.setValue(LoadStatus.INIT_EXCEPTION)
                    } else {
                        mLoadStatusLiveData.setValue(LoadStatus.INIT_FAILED)
                    }
                }
            })
    }

    override fun load(loadType: LoadType) {
        if (columnCollection.value == null) {
            initLoadParams()
            getGameColumnCollection()
        } else if (loadType == LoadType.REFRESH) {
            initLoadParams()
            loadData()
        } else if (loadType == LoadType.RETRY) {
            mLoadStatusLiveData.value = LoadStatus.LIST_LOADED
            loadData()
        } else {
            loadData()
        }
    }

    class Factory(private val collectionId: String) : ViewModelProvider.NewInstanceFactory() {

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