package com.gh.gamecenter.catalog

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.gh.gamecenter.common.entity.ExposureEntity
import com.gh.common.exposure.ExposureUtils
import com.gh.gamecenter.core.utils.UrlFilterUtils
import com.gh.common.view.CatalogFilterView
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.entity.CatalogEntity
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.SubjectSettingEntity
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.Observable
import io.reactivex.Single

class NewCatalogListViewModel(application: Application) : ListViewModel<GameEntity, GameEntity>(application) {

    var title = "" // 显示在 Toolbar 的标题
    var categoryTitle = "" // 跳转进来的分类的标题

    val refresh = MutableLiveData<Boolean>()

    var selectedCatalog = CatalogEntity.SubCatalogEntity()
    var sortType = CatalogFilterView.SortType.RECOMMENDED
    var sortSize = SubjectSettingEntity.Size()

    var primaryCatalogId = "" // 一级分类 id
    private val sensitiveApi = RetrofitManager.getInstance().api

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

    override fun provideDataSingle(page: Int): Single<List<GameEntity>> {
        return if (selectedCatalog.link.type == "column") { // column(专题)/tag(标签)
            sensitiveApi.getColumn(selectedCatalog.link.link, getSortType(), getSortSize(), page) // 专题
        } else {
            sensitiveApi.getGamesWithSpecificTag(getSortSize(), getSortType(), page) // 标签
        }
    }

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

    fun updateSortConfig(
        sortSize: SubjectSettingEntity.Size? = null,
        sortType: CatalogFilterView.SortType? = null,
        sortCatalog: CatalogEntity.SubCatalogEntity? = null
    ) {
        when {
            sortSize != null && sortSize != this.sortSize -> {
                this.sortSize = sortSize
                refresh.postValue(true)
            }

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

            sortCatalog != null && sortCatalog != selectedCatalog -> {
                selectedCatalog = sortCatalog
                refresh.postValue(true)
            }
        }
    }

    private fun getSortSize(): String? {
        return if (selectedCatalog.link.type == "column") {
            UrlFilterUtils.getFilterQuery(
                "min_size", sortSize.min.toString(),
                "max_size", sortSize.max.toString()
            )
        } else {
            UrlFilterUtils.getFilterQuery(
                "tag_id", selectedCatalog.link.link,
                "min_size", sortSize.min.toString(),
                "max_size", sortSize.max.toString()
            )
        }
    }

    private fun getSortType(): String? {
        return when (sortType) {
            CatalogFilterView.SortType.RECOMMENDED -> if (selectedCatalog.link.type == "column") "position:1" else "download:-1"
            CatalogFilterView.SortType.NEWEST -> "publish:-1"
            CatalogFilterView.SortType.RATING -> "star:-1"
        }
    }
}