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.AboutDatabase
import com.jld.vod.interfaces.remote.Apis
import com.jld.vod.model.bean.BaseBean
import com.jld.vod.model.bean.BrowserBean
import com.jld.vod.model.bean.FindAboutAllBean
import com.jld.vod.repository.AboutRepository
import com.jld.vod.repository.BrowserRepository
import com.jld.vod.utils.LogUtils
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 AboutViewModel(app: Application)  : AndroidViewModel(app) {
    private val repository: AboutRepository by lazy { AboutRepository() }
    private val aboutApi = PagingRetrofitUtils.create(Apis::class.java)

    fun getAboutAllList(mid:Long,  lid:Long) : Flow<PagingData<FindAboutAllBean>> {
        return repository.getAboutAllList(aboutApi,mid,lid)
    }

//    val findAboutAlldb = Room.databaseBuilder(app, AboutDatabase::class.java, "AboutAll").build()
//    val findAboutAlldb = Room.inMemoryDatabaseBuilder(app, AboutDatabase::class.java).build()
//    val aboutusList: LiveData<PagedList<FindAboutAllBean>> =
//        findAboutAlldb.aboutDao().getAlls().toLiveData(Config(pageSize = 30,enablePlaceholders = true,maxSize = Int.MAX_VALUE))
//    val findAboutAllliveData: MutableLiveData<BaseBean<List<FindAboutAllBean>>> = MutableLiveData()
//
//    /**
//     * 获取关于信息资源
//     */
//    fun findAboutAll(mid:Long,lid:Long): MutableLiveData<BaseBean<List<FindAboutAllBean>>> {
//        findAboutAllliveData.postValue(BaseBean.loading(null))
//        RetrofitUtils.findAboutAll(mid,lid,
//            { t ->
//                LogUtils.logD("findAboutAll"+t)
//                when (t.code) {
//                    20000 ->{
//                        if (t.data!=null)
//                        {
//                            Thread(Runnable{ run {
//                                if (!findAboutAlldb.aboutDao().getAll().isEmpty())
//                                {
//                                    findAboutAlldb.aboutDao().delete()
//                                    for ((index,e) in t.data.withIndex()){
//                                        findAboutAlldb.aboutDao().insertAll(t.data[index])
//                                    }
//                                }else{
//                                    for ((index,e) in t.data.withIndex()){
//                                        findAboutAlldb.aboutDao().insertAll(t.data[index])
//                                    }
//                                }
//                            }}).start()
//                        }
//                        findAboutAllliveData.postValue(BaseBean.success(t.data))
//                    }
//                    else -> findAboutAllliveData.postValue(BaseBean.error(t.message, t.data))
//                }
//            },
//            { ex ->
//                findAboutAllliveData.postValue(BaseBean.error(ex.message!!, null))
//            }
//        )
//        return findAboutAllliveData
//    }

}