package com.kfzs.duanduan.datashare;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.kfzs.duanduan.datashare.provider.KFZSDDSQLiteOpenHelper;
import com.kfzs.duanduan.datashare.provider.downloadtask.DownloadtaskColumns;
import com.kfzs.duanduan.datashare.provider.session.SessionColumns;

/**
 * <pre>
 *     sinlov
 *
 *     /\__/\
 *    /`    '\
 *  ≈≈≈ 0  0 ≈≈≈ Hello world!
 *    \  --  /
 *   /        \
 *  /          \
 * |            |
 *  \  ||  ||  /
 *   \_oo__oo_/≡≡≡≡≡≡≡≡o
 *
 * </pre>
 * Created by sinlov on 17/4/13.
 */
public class KFZSDDSQLiteOpenHelperCallbacks extends com.kfzs.duanduan.datashare.provider.base.BaseSQLiteOpenHelperCallbacks {

    @Override
    public void onOpen(Context context, SQLiteDatabase db) {
        super.onOpen(context, db);
        Log.i("update", "KFZSDDSQLiteOpenHelper " +
                " - nowVersion: " + db.getVersion());
    }

    @Override
    public void onPostCreate(Context context, SQLiteDatabase db) {
        super.onPostCreate(context, db);
    }

    @Override
    public void onPreCreate(Context context, SQLiteDatabase db) {
        super.onPreCreate(context, db);
    }

    @Override
    public void onUpgrade(Context context, SQLiteDatabase db, int oldVersion, int newVersion) {
        super.onUpgrade(context, db, oldVersion, newVersion);
        Log.i("update", "KFZSDDSQLiteOpenHelper onUpgrade" +
                " oldVersion: " + oldVersion + " |newVersion: " + newVersion);
        for (int i = oldVersion + 1; i <= newVersion; i++) {
            switch (i) {
                case 2:
                    upgradeToVersion2(db);
                    break;
                case 3:
                    upgradeToVersion3(db);
                    break;
                default:
                    // For test version update
//                    upgradeToVersion3(db);
                    Log.w("update", "KFZSDDSQLiteOpenHelper onUpgrade version default most for test, " +
                            "or update data base error! oldVersion: " + oldVersion + " |want version: " + newVersion);
                    break;
            }
        }
    }

    private void upgradeToVersion3(SQLiteDatabase db) {
        String dropSessionTempTable = "DROP TABLE " + SessionColumns.TABLE_NAME + ";";
        db.execSQL(dropSessionTempTable);
        db.execSQL(KFZSDDSQLiteOpenHelper.SQL_CREATE_TABLE_SESSION);
        db.execSQL(KFZSDDSQLiteOpenHelper.SQL_CREATE_INDEX_SESSION_ACCOUNT);
        db.execSQL(KFZSDDSQLiteOpenHelper.SQL_CREATE_INDEX_SESSION_OPEN_ID);
        Log.i("update", "KFZSDDSQLiteOpenHelper onUpgrade version 3 success!");
    }

    private void upgradeToVersion2(SQLiteDatabase db) {
        db.execSQL(KFZSDDSQLiteOpenHelper.SQL_CREATE_TABLE_DOWNLOADTASK);
        String updateIndexDownloadTask = "CREATE INDEX IF NOT EXISTS IDX_DOWNLOADTASK_CREATE_TIME "
                + " ON " + DownloadtaskColumns.TABLE_NAME + " ( " + DownloadtaskColumns.CREATE_TIME + " );";
        db.execSQL(updateIndexDownloadTask);
        String sessionTableRename = "ALTER TABLE " + SessionColumns.TABLE_NAME + " RENAME TO " + SessionColumns.TABLE_NAME + "_temp ;";
        db.execSQL(sessionTableRename);
        db.execSQL(KFZSDDSQLiteOpenHelper.SQL_CREATE_TABLE_SESSION);
        String insertTempSessionTableData = "INSERT INTO " + SessionColumns.TABLE_NAME
                + " ("
                + SessionColumns._ID + ", "
                + SessionColumns.ACCOUNT + ", "
                + SessionColumns.UPDATE_TIME + ", "
                + SessionColumns.TOKEN + ", "
                + SessionColumns.UID + ", "
                + SessionColumns.HASH
                + ")"
                + " SELECT "
                + SessionColumns._ID + ", "
                + SessionColumns.ACCOUNT + ", "
                + SessionColumns.UPDATE_TIME + ", "
                + SessionColumns.TOKEN + ", "
                + SessionColumns.UID + ", "
                + SessionColumns.HASH
                + " FROM " + SessionColumns.TABLE_NAME + "_temp ;";
        db.execSQL(insertTempSessionTableData);
        String dropSessionTempTable = "DROP TABLE " + SessionColumns.TABLE_NAME + "_temp ;";
        db.execSQL(dropSessionTempTable);
        String sessionIndexUpdate = "CREATE INDEX IF NOT EXISTS IDX_SESSION_OPEN_ID "
                + " ON " + SessionColumns.TABLE_NAME + " ( " + SessionColumns.OPEN_ID + " );";
        db.execSQL(sessionIndexUpdate);
        Log.i("update", "KFZSDDSQLiteOpenHelper onUpgrade version 2 success!");
    }
}
