package com.gh.gamecenter.game.commoncollection.detail

import android.app.Application
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.gh.gamecenter.common.baselist.ListViewModel
import com.gh.gamecenter.entity.CommonCollectionEntity
import com.gh.gamecenter.game.data.CommonContentCollectionDetailItem
import com.gh.gamecenter.game.data.CommonContentCollectionDetailOldItem
import com.gh.gamecenter.game.data.CommonContentCollectionDetailRecommendCardItem
import com.gh.gamecenter.home.custom.model.CustomPageItem
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import io.reactivex.Observable

class CustomCommonCollectionDetailViewModel(
    application: Application,
    private val mCollectionId: String,
    cachedLayout: Int,
) : ListViewModel<CommonContentCollectionDetailItem, CommonContentCollectionDetailItem>(application) {

    var finalLayout = cachedLayout
    val commonCollectionLiveData = MutableLiveData<CommonCollectionEntity>()

    fun getLayout(): Int {
        return finalLayout
    }

    override fun provideDataObservable(page: Int): Observable<List<CommonContentCollectionDetailItem>>? {
        return if (page == 1) {
            RetrofitManager.getInstance().api
                .getCommonCollectionDetail(mCollectionId)
                .map {
                    if (it.layout != -1) {
                        finalLayout = it.layout
                    }
                    commonCollectionLiveData.postValue(it)
                    if (it.layout == CustomPageItem.COMMON_CONTENT_COLLECTION_LAYOUT_RECOMMEND_CARD) {
                        it.commonCollectionRecommendCards
                            .map {
                                CommonContentCollectionDetailRecommendCardItem(it)
                            }
                    } else {
                        it.collectionList.map {
                            CommonContentCollectionDetailOldItem(it)
                        }
                    }
                }
        } else {
            if (finalLayout == CustomPageItem.COMMON_CONTENT_COLLECTION_LAYOUT_RECOMMEND_CARD) {
                RetrofitManager.getInstance().api
                    .getCommonCollectionDetailWithRecommenCards(mCollectionId, page)
                    .map {
                        it
                            .map { entity ->
                                CommonContentCollectionDetailRecommendCardItem(entity)
                            }
                            .toMutableList()
                    }
            } else {
                RetrofitManager.getInstance().api
                    .getCommonCollectionDetail(mCollectionId, page)
                    .map {
                        it
                            .map { entity ->
                                CommonContentCollectionDetailOldItem(entity)
                            }
                            .toMutableList()
                    }
            }

        }
    }

    override fun mergeResultLiveData() {
        mResultLiveData.addSource(mListLiveData) { mResultLiveData.postValue(it) }
    }

    class Factory(private val collectionId: String, private val layout: Int) : ViewModelProvider.NewInstanceFactory() {

        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            return CustomCommonCollectionDetailViewModel(HaloApp.getInstance().application, collectionId, layout) as T
        }
    }
}