package com.gh.common.history

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.gh.gamecenter.entity.GamesCollectionEntity
import com.gh.gamecenter.entity.HistoryGameDetailEntity
import com.gh.gamecenter.entity.HistoryGameEntity
import com.gh.gamecenter.entity.MyVideoEntity
import com.gh.gamecenter.feature.entity.AnswerEntity
import com.gh.gamecenter.feature.entity.ArticleEntity
import com.gh.gamecenter.feature.entity.NewsEntity
import com.gh.gamecenter.feature.room.converter.*
import com.gh.gamecenter.room.converter.*
import com.gh.gamecenter.room.dao.*
import com.halo.assistant.HaloApp

@Database(
    entities = [AnswerEntity::class, ArticleEntity::class, NewsEntity::class, HistoryGameEntity::class, MyVideoEntity::class, GamesCollectionEntity::class, HistoryGameDetailEntity::class],
    version = 15,
    exportSchema = false
)
@TypeConverters(
    CountConverter::class,
    CommunityConverter::class,
    TimeConverter::class,
    AnswerUserConverter::class,
    ThumbnailConverter::class,
    TagStyleListConverter::class,
    TagStyleConverter::class,
    StringArrayListConverter::class,
    ListStringConverter::class,
    CommunityVideoConverter::class,
    UserConverter::class,
    ImageInfoConverter::class,
    VideoInfoConverter::class,
    QuestionsConverter::class,
    MeConverter::class,
    SimpleGameListConverter::class,
    TagInfoListConverter::class,
    ActivityLabelListConverter::class,
    IconFloatConverter::class,
    SectionConverter::class
)

abstract class HistoryDatabase : RoomDatabase() {

    abstract fun answerDao(): AnswerHistoryDao
    abstract fun articleDao(): ArticleHistoryDao
    abstract fun newsDao(): NewsHistoryDao
    abstract fun gameDao(): GameDao
    abstract fun videoHistoryDao(): VideoHistoryDao
    abstract fun gamesCollectionDao(): GamesCollectionDao
    abstract fun gameDetailDao(): GameDetailHistoryDao

    companion object {

        val MIGRATION_2_3: Migration = object : Migration(2, 3) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("Alter TABLE HistoryGameEntity add isLibaoExist INTEGER NOT NULL DEFAULT 0")
            }
        }

        val MIGRATION_3_4: Migration = object : Migration(3, 4) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("Alter TABLE AnswerEntity add videos TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE ArticleEntity add videos TEXT NOT NULL DEFAULT ''")
            }
        }

        val MIGRATION_4_5: Migration = object : Migration(4, 5) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("CREATE TABLE MyVideoEntity(id TEXT NOT NULL PRIMARY KEY,poster TEXT NOT NULL DEFAULT '',url TEXT NOT NULL DEFAULT '',vote INTEGER NOT NULL DEFAULT 0,length INTEGER NOT NULL DEFAULT 0,time INTEGER NOT NULL DEFAULT 0,videoStreamRecord INTEGER NOT NULL DEFAULT 0,status TEXT NOT NULL DEFAULT '')")
            }
        }

        val MIGRATION_5_6: Migration = object : Migration(5, 6) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("Alter TABLE MyVideoEntity add title TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE MyVideoEntity add commentCount INTEGER NOT NULL DEFAULT 0")
                database.execSQL("Alter TABLE MyVideoEntity add user TEXT NOT NULL DEFAULT ''")
            }
        }

        val MIGRATION_6_7: Migration = object : Migration(6, 7) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("Alter TABLE HistoryGameEntity add iconSubscript TEXT DEFAULT ''")
            }
        }

        val MIGRATION_7_8: Migration = object : Migration(7, 8) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("Alter TABLE ArticleEntity add images TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE ArticleEntity add imagesInfo TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE AnswerEntity add imagesInfo TEXT NOT NULL DEFAULT ''")
            }
        }

        val MIGRATION_8_9: Migration = object : Migration(8, 9) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("Alter TABLE ArticleEntity add des TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE ArticleEntity add url TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE ArticleEntity add videoInfo TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE ArticleEntity add poster TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE ArticleEntity add length INTEGER NOT NULL DEFAULT 0")
                database.execSQL("Alter TABLE ArticleEntity add status TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE ArticleEntity add content TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE ArticleEntity add questions TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE AnswerEntity add des TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE AnswerEntity add url TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE AnswerEntity add videoInfo TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE AnswerEntity add poster TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE AnswerEntity add length INTEGER NOT NULL DEFAULT 0")
                database.execSQL("Alter TABLE AnswerEntity add status TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE AnswerEntity add content TEXT NOT NULL DEFAULT ''")
            }
        }

        val MIGRATION_9_10: Migration = object : Migration(9, 10) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL(
                    "CREATE TABLE GamesCollectionEntity (id TEXT NOT NULL PRIMARY KEY, tags TEXT, games TEXT, title TEXT NOT NULL, intro TEXT NOT NULL, cover TEXT NOT NULL, display TEXT NOT NULL, stamp TEXT NOT NULL, count TEXT, user TEXT, me TEXT, orderTag INTEGER NOT NULL)"
                )
            }
        }

        val MIGRATION_10_11: Migration = object : Migration(10, 11) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("Alter TABLE GamesCollectionEntity add activityTags TEXT DEFAULT ''")
            }
        }

        val MIGRATION_11_12: Migration = object : Migration(11, 12) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("Alter TABLE HistoryGameEntity add subtitle TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE HistoryGameEntity add subtitleStyle TEXT")
            }
        }

        val MIGRATION_12_13: Migration = object : Migration(12, 13) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("Alter TABLE HistoryGameEntity add iconFloat TEXT")
            }
        }

        val MIGRATION_13_14: Migration = object : Migration(13, 14) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("Alter TABLE ArticleEntity add type TEXT NOT NULL DEFAULT 'community_article'")
                database.execSQL("Alter TABLE ArticleEntity add sections TEXT NOT NULL DEFAULT ''")
                database.execSQL("Alter TABLE ArticleEntity add tagActivityName TEXT NOT NULL DEFAULT ''")
            }
        }

        val MIGRATION_14_15: Migration = object : Migration(14, 15) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("CREATE TABLE HistoryGameDetailEntity (id TEXT NOT NULL PRIMARY KEY, name TEXT DEFAULT '')")
            }
        }

        val instance by lazy {
            Room.databaseBuilder(
                HaloApp.getInstance().application,
                HistoryDatabase::class.java,
                "USER_TRACK_HISTORY_DATABASE"
            )
                .addMigrations(MIGRATION_2_3)
                .addMigrations(MIGRATION_3_4)
                .addMigrations(MIGRATION_4_5)
                .addMigrations(MIGRATION_5_6)
                .addMigrations(MIGRATION_6_7)
                .addMigrations(MIGRATION_7_8)
                .addMigrations(MIGRATION_8_9)
                .addMigrations(MIGRATION_9_10)
                .addMigrations(MIGRATION_10_11)
                .addMigrations(MIGRATION_11_12)
                .addMigrations(MIGRATION_12_13)
                .addMigrations(MIGRATION_13_14)
                .addMigrations(MIGRATION_14_15)
                .build()
        }
    }

}