package com.gh.gamecenter.gamedetail.fuli.kaifu

import android.app.Application
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.feature.entity.ServerCalendarGame
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.Observable
import io.reactivex.Single

class ServersCalendarRemindListViewModel(application: Application) :
    ListViewModel<ServersCalendarRemindListViewModel.ItemData, ServersCalendarRemindListViewModel.ItemData>(application) {

    override fun provideDataObservable(page: Int): Observable<List<ItemData>>? {
        return null
    }

    override fun provideDataSingle(page: Int): Single<List<ItemData>> {
        return RetrofitManager.getInstance().newApi
            .getServerCalendarRemindList(page, 20)
            .map { list ->
                val dataList = mutableListOf<ItemData>()
                list.forEach { game ->
                    if (game.servers.isNotEmpty()) {
                        dataList.add(ItemData(game = game))
                    }
                }
                dataList
            }
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { list ->
            mResultLiveData.postValue(
                mutableListOf<ItemData>()
                    .also {
                        it.addAll(list)
                        it.add(ItemData(footer = true))
                    }
            )
        }
    }

    data class ItemData(val game: ServerCalendarGame? = null, val footer: Boolean = false)
}