package com.renrenliao.im.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
//import com.renrenliao.im.bean.AvatarTAG
import com.renrenliao.im.bean.UserInfo
import com.renrenliao.im.im.AlarmEntity
import com.renrenliao.im.im.GroupEntity
import com.renrenliao.im.im.ImMsgBean


/**
 *Author:ganzhe
 *时间:2020/11/9 16:05
 *描述:This is AppDatabase
 */

@Database(entities = [UserInfo::class,GroupEntity::class, ImMsgBean::class,AlarmEntity::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {

   // abstract fun studentDao(): StudentDao
    abstract fun friendDao(): FriendDao
    abstract fun groupDao(): GroupDao
    abstract fun msgDao(): ImMsgDao
    abstract fun alarmDao(): AlarmDao
    //abstract fun avatarTagDao(): AvatarTagDao

    companion object {

        @Volatile
        private var INSTANCE: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase =/*{
            if (INSTANCE ==null){
                synchronized(this){
                    if (INSTANCE == null){
                        INSTANCE = buildDatabase(context)
                    }
                }
            }
            return INSTANCE!!
        }*/

            INSTANCE ?: synchronized(this) {
                INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
            }

        private fun buildDatabase(context: Context) =
            Room.databaseBuilder(context, AppDatabase::class.java, "chat.db")
                //     .allowMainThreadQueries()
                .build()
    }
}