package com.satsumasoftware.timetable.db;

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

import com.satsumasoftware.timetable.db.util.SchemaUtilsKt;

public final class TimetableDbHelper extends SQLiteOpenHelper {

    private static TimetableDbHelper sInstance;

    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_NAME = "Timetable.db";

    private static final String LOG_TAG = "TimetableDbHelper";

    public static synchronized TimetableDbHelper getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new TimetableDbHelper(context.getApplicationContext());
        }
        return sInstance;
    }

    private TimetableDbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(LOG_TAG, "onCreate() called");

        db.execSQL(AssignmentsSchema.SQL_CREATE);
        db.execSQL(ClassDetailsSchema.SQL_CREATE);
        db.execSQL(ClassesSchema.SQL_CREATE);
        db.execSQL(ClassTimesSchema.SQL_CREATE);
        db.execSQL(ExamsSchema.SQL_CREATE);
        db.execSQL(SubjectsSchema.SQL_CREATE);
        db.execSQL(TermsSchema.SQL_CREATE);
        db.execSQL(TimetablesSchema.SQL_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(LOG_TAG, "onUpgrade() called with oldVersion " + oldVersion +
                " and newVersion " + newVersion);

        /*
         * Note that there are no breaks in the switch statement below (except for the last case).
         *
         * This means, for example, if installed version is 2 onUpgrade will get called with
         * oldVersion as 2. The switch case will execute cases 2, 3, 4, etc as the break
         * statements are missing in all the older cases.
         */

        switch (oldVersion) {
            case 1:
                db.execSQL("ALTER TABLE " + SubjectsSchema.TABLE_NAME + " ADD COLUMN " +
                        SubjectsSchema.COL_ABBREVIATION + SchemaUtilsKt.TEXT_TYPE + " DEFAULT ''");
                break;

            default:
                throw new IllegalArgumentException("onUpgrade() called with unknown oldVersion "
                        + oldVersion);
        }
    }

}

@@ -249,16 +249,105 @@
 UtilsKt;
+%0Aimport com.satsumasoftware.timetable.framework.Class;%0A%0Aimport org.threeten.bp.LocalDate;
 %0A%0Apublic
@@ -502,17 +502,17 @@
 RSION = 
-2
+3
 ;%0A    pr
@@ -2314,16 +2314,1554 @@
 LT ''%22);
+%0A%0A            case 2:%0A                LocalDate defaultDate = Class.NO_DATE;%0A%0A                db.execSQL(%22ALTER TABLE %22 + ClassesSchema.TABLE_NAME + %22 ADD COLUMN %22 +%0A                        ClassesSchema.COL_START_DATE_DAY_OF_MONTH + SchemaUtilsKt.INTEGER_TYPE +%0A                        %22 DEFAULT %22 + defaultDate.getDayOfMonth());%0A                db.execSQL(%22ALTER TABLE %22 + ClassesSchema.TABLE_NAME + %22 ADD COLUMN %22 +%0A                        ClassesSchema.COL_START_DATE_MONTH + SchemaUtilsKt.INTEGER_TYPE +%0A                        %22 DEFAULT %22 + defaultDate.getMonth());%0A                db.execSQL(%22ALTER TABLE %22 + ClassesSchema.TABLE_NAME + %22 ADD COLUMN %22 +%0A                        ClassesSchema.COL_START_DATE_YEAR + SchemaUtilsKt.INTEGER_TYPE +%0A                        %22 DEFAULT %22 + defaultDate.getYear());%0A%0A                db.execSQL(%22ALTER TABLE %22 + ClassesSchema.TABLE_NAME + %22 ADD COLUMN %22 +%0A                        ClassesSchema.COL_END_DATE_DAY_OF_MONTH + SchemaUtilsKt.INTEGER_TYPE +%0A                        %22 DEFAULT %22 + defaultDate.getDayOfMonth());%0A                db.execSQL(%22ALTER TABLE %22 + ClassesSchema.TABLE_NAME + %22 ADD COLUMN %22 +%0A                        ClassesSchema.COL_END_DATE_MONTH + SchemaUtilsKt.INTEGER_TYPE +%0A                        %22 DEFAULT %22 + defaultDate.getMonth());%0A                db.execSQL(%22ALTER TABLE %22 + ClassesSchema.TABLE_NAME + %22 ADD COLUMN %22 +%0A                        ClassesSchema.COL_END_DATE_YEAR + SchemaUtilsKt.INTEGER_TYPE +%0A                        %22 DEFAULT %22 + defaultDate.getYear());
 %0A       

Update the existing db by adding some columns for dates