package com.jld.vod.viewmodel

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.paging.Config
import androidx.paging.PagedList
import androidx.paging.PagingData
import androidx.paging.toLiveData
import androidx.room.Room
import com.jld.vod.db.GameDatabase
import com.jld.vod.interfaces.remote.Apis
import com.jld.vod.model.bean.BaseBean

import com.jld.vod.model.bean.GameBean
import com.jld.vod.repository.GameRepository
import com.jld.vod.utils.http.PagingRetrofitUtils
import com.jld.vod.utils.http.RetrofitUtils
import kotlinx.coroutines.flow.Flow


/**
 * @author ZhaoFuXin
 * @Email：18276061387@163.com
 * @description:
 * @date :2020/1/10 13:40
 */
class GameViewModel(app: Application)  : AndroidViewModel(app) {
    private val repository: GameRepository by lazy { GameRepository() }
    private val gameApi = PagingRetrofitUtils.create(Apis::class.java)

    fun getGameAllList(mid:Long,  lid:Long, terminal :String) : Flow<PagingData<GameBean>> {
        return repository.getGameAllList(gameApi,mid,lid,terminal)
    }

    //val gamedb = Room.inMemoryDatabaseBuilder(app, GameDatabase::class.java, "game").build()
 //   val gamedb = Room.inMemoryDatabaseBuilder(app, GameDatabase::class.java).build()
//    val gameList: LiveData<PagedList<GameBean>> =
//        gamedb.gameDao().getAlls().toLiveData(Config(pageSize = 30,enablePlaceholders = true,maxSize = Int.MAX_VALUE))

//    val findGameAllliveData: MutableLiveData<BaseBean<List<GameBean>>> = MutableLiveData()

    /**
     * 获取所有游戏信息
     */
//    fun findGameAll(mid:Long,lid:Long,terminal:String): MutableLiveData<BaseBean<List<GameBean>>> {
//        findGameAllliveData.postValue(BaseBean.loading(null))
//        RetrofitUtils.findGameAll(mid,lid,terminal,
//            { t ->
//                when (t.code) {
//                    20000 ->{
//                        if (t.data!=null)
//                        {
//                            Thread(Runnable{ run {
//                                if (!gamedb.gameDao().getAll().isEmpty())
//                                {
//                                    gamedb.gameDao().delete()
//                                    for ((index,e) in t.data.withIndex()){
//                                        gamedb.gameDao().insertAll(t.data[index])
//                                    }
//                                }else{
//                                    for ((index,e) in t.data.withIndex()){
//                                        gamedb.gameDao().insertAll(t.data[index])
//                                    }
//                                }
//                            }}).start()
//                        }
//                        findGameAllliveData.postValue(BaseBean.success(t.data))
//                    }
//                    else -> findGameAllliveData.postValue(BaseBean.error(t.message, t.data))
//                }
//            },
//            { ex ->
//                findGameAllliveData.postValue(BaseBean.error(ex.message!!, null))
//            }
//        )
//        return findGameAllliveData
//    }

}