package com.gh.gamecenter.category2

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.common.exposure.ExposureUtils
import com.gh.common.util.AdHelper
import com.gh.common.view.CategoryFilterView
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.entity.ExposureEntity
import com.gh.gamecenter.common.exposure.ExposureSource
import com.gh.gamecenter.core.utils.UrlFilterUtils
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.feature.exposure.ExposureConstants
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 exposureSourceList: List<ExposureSource>?
) : ListViewModel<GameEntity, GameEntity>(application) {

    val refresh = MutableLiveData<Boolean>()

    var gameFiltered = CategoryV2ViewModel.GameFiltered()
        private set

    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(), AdHelper.getIdfaString(), page)
    }

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

    fun updateSortConfig(
        gameFiltered: CategoryV2ViewModel.GameFiltered
    ) {
        this.gameFiltered = gameFiltered
        refresh.postValue(true)
    }

    private fun getFilter(): String? {

        return UrlFilterUtils.getFilterQuery(
            "category_ids", gameFiltered.categoryIds.ifBlank { gameFiltered.sidebarCategoryId },
            "min_size", gameFiltered.size.min.toString(),
            "max_size", gameFiltered.size.max.toString()
        )
    }

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

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