package com.gh.gamecenter.gamecollection.square

import android.annotation.SuppressLint
import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.gh.common.filter.RegionSettingHelper
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.common.baselist.LoadParams
import com.gh.gamecenter.common.baselist.LoadStatus
import com.gh.gamecenter.common.baselist.LoadType
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.entity.*
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.schedulers.Schedulers
import java.util.*

class GameCollectionSquareViewModel(application: Application) :
    ListViewModel<GamesCollectionEntity, GameCollectionListItemData>(application) {

    var entrance: String? = null
    var selectedTagEntity: TagInfoEntity? = null
    var selectedTagId = ""
    var selectedTagName = "全部标签"
    var view = "recommend"
    var isHome = false

    val mAmwayCommentList = MutableLiveData<List<AmwayCommentEntity>>()
    val mBannerList = MutableLiveData<List<CarouselEntity>>()

    override fun load(loadType: LoadType?) {
        if (loadType == LoadType.REFRESH) mResultLiveData.postValue(arrayListOf())
        super.load(loadType)
    }

    override fun loadData() {
        if (isHome) getAmwayCommentList()
        getBannerList()
        super.loadData()
    }

    override fun initLoadParams() {
        mCurLoadParams = LoadParams(PAGE_SIZE, LoadParams.DEFAULT_OFFSET)
        mLoadStatusLiveData.value = LoadStatus.INIT
    }

    override fun provideDataSingle(page: Int): Single<MutableList<GamesCollectionEntity>> =
        if (isHome)
            RetrofitManager.getInstance()
                .api.getHomeGameCollectionSquareList(UUID.randomUUID().toString(), page, PAGE_SIZE)
        else
            RetrofitManager.getInstance()
                .api.getGameCollectionSquareList(view, selectedTagId, page, PAGE_SIZE)

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { list ->
            val itemDataList = arrayListOf<GameCollectionListItemData>().apply {
                var position = 0
                if (isHome) add(
                    GameCollectionListItemData(
                        amwayListItem = mAmwayCommentList.value,
                        carouselListItem = mBannerList.value
                    )
                )
                for (item in list) {
                    item.games = RegionSettingHelper.filterSimpleGame(item.games)
                    add(GameCollectionListItemData(gameCollectionItem = item, gameStartPosition = position))
                    position += if (item.count?.game!! > 2) 3 else if (item.games?.size == 0) 0 else item.count?.game
                        ?: 0
                }
            }
            mResultLiveData.postValue(itemDataList)
        }
    }

    override fun provideDataObservable(page: Int): Observable<MutableList<GamesCollectionEntity>>? = null

    @SuppressLint("CheckResult")
    fun getAmwayCommentList() {
        RetrofitManager.getInstance().api
            .getAmwayCommentList(1, 10)
            .subscribeOn(Schedulers.io())
            .subscribe(object : BiResponse<List<AmwayCommentEntity>>() {
                override fun onSuccess(data: List<AmwayCommentEntity>) {
                    mAmwayCommentList.postValue(data)
                }
            })
    }

    @SuppressLint("CheckResult")
    fun getBannerList() {
        RetrofitManager.getInstance().api
            .gameCollectionSquareBanner
            .subscribeOn(Schedulers.io())
            .subscribe(object : BiResponse<List<CarouselEntity>>() {
                override fun onSuccess(data: List<CarouselEntity>) {
                    mBannerList.postValue(data)
                }

                override fun onFailure(exception: Exception) {
                    mBannerList.postValue(emptyList())
                }
            })
    }

    companion object {
        const val PAGE_SIZE = 15
    }
}