package com.gh.gamecenter.category2

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.entity.ExposureEntity
import com.gh.common.exposure.ExposureSource
import com.gh.common.exposure.ExposureUtils
import com.gh.gamecenter.core.utils.UrlFilterUtils
import com.gh.common.view.CategoryFilterView
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.SubjectSettingEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Observable
import io.reactivex.Single

class CategoryV2ListViewModel(
    application: Application,
    val categoryId: String,
    var categoryIds: String,
    var exposureSourceList: List<ExposureSource>?
) : ListViewModel<GameEntity, GameEntity>(application) {

    val refresh = MutableLiveData<Boolean>()

    var sortType = CategoryFilterView.SortType.RECOMMENDED
    var sortSize = SubjectSettingEntity.Size()

    override fun provideDataObservable(page: Int): Observable<List<GameEntity>>? = null

    override fun provideDataSingle(page: Int): Single<List<GameEntity>> {
        return RetrofitManager
            .getInstance()
            .api
            .getCategoryV2Games(categoryId, getFilter(), getSortType(), page)
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) {
            ExposureUtils.updateExposureInfo(
                gameList = it,
                containerId = categoryId,
                containerType = ExposureEntity.CATEGORY_V2_ID
            )
            it.forEach { game -> game.hideSizeInsideDes = true }
            mResultLiveData.postValue(it)
        }
    }

    fun updateSortConfig(
        sortSize: SubjectSettingEntity.Size? = null,
        sortType: CategoryFilterView.SortType? = null,
        categoryIds: String? = null
    ) {
        when {
            sortSize != null && sortSize != this.sortSize -> {
                this.sortSize = sortSize
                refresh.postValue(true)
            }

            sortType != null && sortType != this.sortType -> {
                this.sortType = sortType
                refresh.postValue(true)
            }

            categoryIds != null && categoryIds != this.categoryIds -> {
                this.categoryIds = categoryIds
                refresh.postValue(true)
            }
        }
    }

    private fun getFilter(): String? {
        return UrlFilterUtils.getFilterQuery(
            "category_ids", categoryIds,
            "min_size", sortSize.min.toString(),
            "max_size", sortSize.max.toString()
        )

    }

    private fun getSortType(): String? {
        return when (sortType) {
            CategoryFilterView.SortType.RECOMMENDED -> "download:-1"
            CategoryFilterView.SortType.NEWEST -> "publish:-1"
            CategoryFilterView.SortType.RATING -> "star:-1"
        }
    }

    class Factory(val categoryId: String, val categoryIds: String, val exposureSourceList: List<ExposureSource>?) :
        ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return CategoryV2ListViewModel(
                HaloApp.getInstance().application,
                categoryId,
                categoryIds,
                exposureSourceList
            ) as T
        }
    }
}