package com.jld.vod.viewmodel

import android.app.Application
import android.util.Log
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.paging.Config
import androidx.paging.PagedList
import androidx.paging.toLiveData
import androidx.room.Room
import com.jld.vod.db.SettingDataBase
import com.jld.vod.model.bean.SettingBean
import kotlin.collections.ArrayList

class  SettingViewModel(app: Application)  : AndroidViewModel(app){
    private  var app: Application? = null
    init{ this.app = app }

    private val settingDb = Room.databaseBuilder(app, SettingDataBase::class.java, "setting").build()

    val settingViewList: LiveData<PagedList<SettingBean>> =
        settingDb.settingDao().getAlls().toLiveData(Config(pageSize = 30,enablePlaceholders = true,maxSize = Int.MAX_VALUE))


    //存入数据
    fun addSettingData() {
        val emptyList = ArrayList<Map<String, String>>()
        emptyList.add(hashMapOf("id" to "1", "name" to "开启联动", "img" to "1"))
        emptyList.add(hashMapOf("id" to "2", "name" to "关闭联动", "img" to "1"))
        emptyList.add(hashMapOf("id" to "3", "name" to "清除座位信息", "img" to "1"))
        emptyList.add(hashMapOf("id" to "4", "name" to "编辑座位号", "img" to "1"))

        if (settingDb.settingDao().getAll().isNotEmpty()) {
            //不为空
            settingDb.settingDao().delete()
            for ((index,e) in emptyList.withIndex()){
              settingDb.settingDao().insertAll(SettingBean(null,
                    e["id"]?.toInt(), e["name"], e["img"]?.toInt()))
            }
        }else{
            for ((index,e) in emptyList.withIndex()){
                settingDb.settingDao().insertAll(SettingBean(null,
                    e["id"]?.toInt(), e["name"], e["img"]?.toInt()))
            }
        }

    }

}

