package com.ys.bdtp.adm.mvvm.model.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.ys.bdtp.adm.mvvm.model.db.converter.GsonConverter
import com.ys.bdtp.adm.mvvm.model.db.dao.*
import com.ys.bdtp.adm.mvvm.model.db.dao.config.*
import com.ys.bdtp.adm.mvvm.model.db.dao.dict.*
import com.ys.bdtp.adm.mvvm.model.db.dao.task.*
import com.ys.bdtp.adm.mvvm.model.db.entity.UserEntity
import com.ys.bdtp.adm.mvvm.model.db.entity.config.*
import com.ys.bdtp.adm.mvvm.model.db.entity.dict.*
import com.ys.bdtp.adm.mvvm.model.db.entity.task.*


@Database(
    entities = [
        // config
        ComponentConfigEntity::class,
        ConfigEntity::class,
        ContainerConfigEntity::class,
        InfosConfigEntity::class,
        PipeConfigEntity::class,
        ProblemInfoConfigEntity::class,
        ProblemTypeConfigEntity::class,
        RelationConfigEntity::class,

        // dict
        EquipmentDictEntity::class,
        InfoDictEntity::class,
        MajorDictEntity::class,
        RelationDictEntity::class,
        SystemDictEntity::class,
        // task
        BuildingEntity::class,
        CadEntity::class,
        CustomInfoEntity::class,
        EquipmentEntity::class,
        FileEntity::class,
        FloorEntity::class,
        PipeEntity::class,
        ProblemEntity::class,
        ProjectEntity::class,
        QrCodeEntity::class,
        RelationEntity::class,
        ServeAreaEntity::class,
        SpaceEntity::class,
        WorkspaceEntity::class,
        // user
        UserEntity::class
    ], version = 1, exportSchema = true
)
@TypeConverters(GsonConverter::class)
abstract class AdmDatabase : RoomDatabase() {
    // config
    abstract fun componentConfigDao(): ComponentConfigDao
    abstract fun configDao(): ConfigDao
    abstract fun containerConfigDao(): ContainerConfigDao
    abstract fun infosConfigDao(): InfosConfigDao
    abstract fun pipeConfigDao(): PipeConfigDao
    abstract fun problemInfoConfigDao(): ProblemInfoConfigDao
    abstract fun problemTypeConfigDao(): ProblemTypeDao
    abstract fun relationConfigDao(): RelationConfigDao

    // dict
    abstract fun equipmentDictDao(): EquipmentDictDao
    abstract fun infoDictDao(): InfoDictDao
    abstract fun majorDictDao(): MajorDictDao
    abstract fun relationDictDao(): RelationDictDao
    abstract fun systemDictDao(): SystemDictDao

    // mission
    abstract fun buildingDao(): BuildingDao
    abstract fun cadDao(): CadDao
    abstract fun customInfoDao(): CustomInfoDao
    abstract fun equipmentDao(): EquipmentDao
    abstract fun fileDao(): FileDao
    abstract fun floorDao(): FloorDao
    abstract fun pipeDao(): PipeDao
    abstract fun problemDao(): ProblemDao
    abstract fun projectDao(): ProjectDao
    abstract fun qrCodeDao(): QrCodeDao
    abstract fun relationDao(): RelationDao
    abstract fun serveAreaDao(): ServeAreaDao
    abstract fun spaceDao(): SpaceDao
    abstract fun workspaceDao(): WorkspaceDao

    // user
    abstract fun userDao(): UserDao

    companion object {
        @Volatile
        private var instance: AdmDatabase? = null

        operator fun invoke(context: Context): AdmDatabase {
            return instance ?: synchronized(this) {
                instance ?: Room.databaseBuilder(
                    context,
                    AdmDatabase::class.java,
                    "adm_db"
                ).build().also { instance = it }
            }
        }
    }
}