package com.gh.gamecenter.room;

import androidx.annotation.NonNull;
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.common.videolog.VideoRecordDao;
import com.gh.common.videolog.VideoRecordEntity;
import com.gh.gamecenter.entity.CommentDraft;
import com.gh.gamecenter.entity.ForumEntity;
import com.gh.gamecenter.entity.GameCollectionDraft;
import com.gh.gamecenter.entity.HomePluggableFilterEntity;
import com.gh.gamecenter.entity.SignEntity;
import com.gh.gamecenter.feature.entity.SimulatorGameRecordEntity;
import com.gh.gamecenter.packagehelper.FilterPackageNameEntity;
import com.gh.gamecenter.packagehelper.PackageFilterDao;
import com.gh.gamecenter.feature.entity.AnswerEntity;
import com.gh.gamecenter.room.converter.ActivityLabelListConverter;
import com.gh.gamecenter.room.converter.ApkArrayListConverter;
import com.gh.gamecenter.room.converter.MeConverter;
import com.gh.gamecenter.room.converter.SimpleGameConverter;
import com.gh.gamecenter.room.converter.SimpleGameListConverter;
import com.gh.gamecenter.room.converter.SimulatorConverter;
import com.gh.gamecenter.room.converter.StringArrayListConverter;
import com.gh.gamecenter.room.converter.TagInfoListConverter;
import com.gh.gamecenter.room.converter.TagStyleListConverter;
import com.gh.gamecenter.feature.room.converter.VideoInfoConverter;
import com.gh.gamecenter.room.dao.AnswerDao;
import com.gh.gamecenter.room.dao.CommentDraftDao;
import com.gh.gamecenter.room.dao.ForumDao;
import com.gh.gamecenter.room.dao.GameCollectionDraftDao;
import com.gh.gamecenter.room.dao.HomePluggableFilterDao;
import com.gh.gamecenter.room.dao.SignDao;
import com.gh.gamecenter.room.dao.SimulatorGameDao;
import com.gh.gamecenter.room.dao.UploadDao;
import com.gh.gamecenter.video.upload.UploadEntity;
import com.halo.assistant.HaloApp;

/**
 * Created by khy on 28/11/17.
 */
@Database(entities = {
        SignEntity.class,
        AnswerEntity.class,
        UploadEntity.class,
        CommentDraft.class,
        HomePluggableFilterEntity.class,
        VideoRecordEntity.class,
        SimulatorGameRecordEntity.class,
        ForumEntity.class,
        GameCollectionDraft.class,
        FilterPackageNameEntity.class
}, version = 23, exportSchema = false)
@TypeConverters({
        StringArrayListConverter.class,
        TagStyleListConverter.class,
        SimulatorConverter.class,
        ApkArrayListConverter.class,
        SimpleGameConverter.class,
        VideoInfoConverter.class,
        MeConverter.class,
        SimpleGameListConverter.class,
        TagInfoListConverter.class,
        ActivityLabelListConverter.class
})
public abstract class AppDatabase extends RoomDatabase {

    public abstract AnswerDao answerDao();

    public abstract SignDao signDao();

    public abstract UploadDao uploadDao();

    public abstract CommentDraftDao commentDraftDao();

    public abstract HomePluggableFilterDao homePluggableFilterDao();

    public abstract VideoRecordDao videoRecordDao();

    public abstract SimulatorGameDao simulatorGameDao();

    public abstract ForumDao forumDao();

    public abstract GameCollectionDraftDao gameCollectionDraftDao();

    public abstract PackageFilterDao packageFilterDao();

    private static final String DATABASE_NAME = "gh-db";

    public static AppDatabase getInstance() {
        return SingletonHolder.INSTANCE;
    }

    static final Migration MIGRATION_2_4 = new Migration(2, 4) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("CREATE TABLE SignEntity(id TEXT NOT NULL PRIMARY KEY, experience INTEGER NOT NULL, serialSign INTEGER NOT NULL, coefficients INTEGER NOT NULL, lastTime INTEGER NOT NULL, title TEXT, data TEXT)");
            database.execSQL("CREATE TABLE AnswerEntity(primaryKey TEXT NOT NULL PRIMARY KEY, communityId TEXT, orderTag INTEGER NOT NULL, id TEXT, sequenceId TEXT, brief TEXT, images TEXT NOT NULL, vote INTEGER NOT NULL, user TEXT NOT NULL, questions TEXT NOT NULL, communityName TEXT, commentCount INTEGER NOT NULL)");
        }
    };

    static final Migration MIGRATION_4_5 = new Migration(4, 5) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("Alter TABLE AnswerEntity add active INTEGER NOT NULL DEFAULT 1");
        }
    };

    static final Migration MIGRATION_5_6 = new Migration(5, 6) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
        }
    };


    static final Migration MIGRATION_6_7 = new Migration(6, 7) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("Alter TABLE AnswerEntity add articleTitle TEXT");
            database.execSQL("Alter TABLE AnswerEntity add type TEXT NOT NULL DEFAULT ''");
        }
    };

    static final Migration MIGRATION_7_8 = new Migration(7, 8) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE AnswerEntity add time INTEGER");
        }
    };

    static final Migration MIGRATION_8_9 = new Migration(8, 9) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
        }
    };

    static final Migration MIGRATION_9_10 = new Migration(9, 10) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
        }
    };

    static final Migration MIGRATION_10_11 = new Migration(10, 11) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("Alter TABLE AnswerEntity add videos TEXT NOT NULL DEFAULT ''");
            database.execSQL("CREATE TABLE UploadEntity(uploadFilePath TEXT NOT NULL PRIMARY KEY, domain TEXT NOT NULL, `key` TEXT NOT NULL, success INTEGER NOT NULL)");
        }
    };

    static final Migration MIGRATION_11_12 = new Migration(11, 12) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
        }
    };

    static final Migration MIGRATION_12_13 = new Migration(12, 13) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("CREATE TABLE CommentDraft(id TEXT NOT NULL PRIMARY KEY, draft TEXT NOT NULL)");
        }
    };

    static final Migration MIGRATION_13_14 = new Migration(13, 14) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("Alter TABLE UploadEntity add fileMD5 TEXT");
        }
    };

    static final Migration MIGRATION_14_15 = new Migration(14, 15) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("CREATE TABLE HomePluggableFilterEntity (pkgName TEXT NOT NULL PRIMARY KEY, tag TEXT NOT NULL, active INTEGER NOT NULL)");
            database.execSQL("CREATE TABLE VideoRecord (id TEXT NOT NULL PRIMARY KEY, videoId TEXT NOT NULL, time INTEGER NOT NULL)");
        }
    };

    static final Migration MIGRATION_15_16 = new Migration(15, 16) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
        }
    };

    static final Migration MIGRATION_16_17 = new Migration(16, 17) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("Alter TABLE AnswerEntity add imagesInfo TEXT NOT NULL DEFAULT ''");
        }
    };

    static final Migration MIGRATION_17_18 = new Migration(17, 18) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("CREATE TABLE SimulatorGameRecordEntity (id TEXT NOT NULL PRIMARY KEY, icon TEXT, iconSubscript TEXT, " +
                    "name TEXT, brief TEXT, tag TEXT, isLibaoExist INTEGER NOT NULL DEFAULT 0, tagStyle TEXT NOT NULL, orderTag INTEGER NOT NULL DEFAULT 0, " +
                    "des TEXT, apk TEXT, apkNormal TEXT, simulatorType TEXT NOT NULL DEFAULT '', simulator TEXT, category TEXT NOT NULL DEFAULT '', " +
                    "isRecentlyPlayed INTEGER NOT NULL DEFAULT 0, isDeleted INTEGER NOT NULL DEFAULT 0)");
        }
    };

    static final Migration MIGRATION_18_19 = new Migration(18, 19) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("CREATE TABLE ForumEntity (id TEXT NOT NULL PRIMARY KEY, game TEXT NOT NULL, name TEXT NOT NULL, icon TEXT NOT NULL, isFollow INTEGER NOT NULL, isRecommend INTEGER NOT NULL, orderTag INTEGER NOT NULL, unread INTEGER NOT NULL, type TEXT NOT NULL, me TEXT NOT NULL)");
            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 ''");
        }
    };
    static final Migration MIGRATION_19_20 = new Migration(19, 20) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("Alter TABLE CommentDraft add pictureList TEXT DEFAULT ''");
        }
    };

    static final Migration MIGRATION_20_21 = new Migration(20, 21) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("CREATE TABLE GameCollectionDraft(primaryKey TEXT NOT NULL PRIMARY KEY, tags TEXT DEFAULT '', games TEXT DEFAULT '' , title TEXT NOT NULL DEFAULT '', intro TEXT NOT NULL DEFAULT '', cover TEXT NOT NULL DEFAULT '', display TEXT NOT NULL DEFAULT '')");
        }
    };

    static final Migration MIGRATION_21_22 = new Migration(21, 22) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("CREATE TABLE FilterPackageNameEntity(packageName TEXT NOT NULL PRIMARY KEY)");
        }
    };

    static final Migration MIGRATION_22_23 = new Migration(22, 23) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            database.execSQL("Alter TABLE GameCollectionDraft add activityTags TEXT DEFAULT ''");
        }
    };

    private static class SingletonHolder {
        private static final AppDatabase INSTANCE =
                Room.databaseBuilder(HaloApp.getInstance().getApplicationContext(), AppDatabase.class, DATABASE_NAME)
                        .addMigrations(
                                MIGRATION_2_4,
                                MIGRATION_4_5,
                                MIGRATION_5_6,
                                MIGRATION_6_7,
                                MIGRATION_7_8,
                                MIGRATION_8_9,
                                MIGRATION_9_10,
                                MIGRATION_10_11,
                                MIGRATION_11_12,
                                MIGRATION_12_13,
                                MIGRATION_13_14,
                                MIGRATION_14_15,
                                MIGRATION_15_16,
                                MIGRATION_16_17,
                                MIGRATION_17_18,
                                MIGRATION_18_19,
                                MIGRATION_19_20,
                                MIGRATION_20_21,
                                MIGRATION_21_22,
                                MIGRATION_22_23
                        )
                        // 不允许主线程查询
                        .allowMainThreadQueries()
//                // 提供db升级的策略而不是强行销毁
//                .fallbackToDestructiveMigration()
                        .build();
    }
}
