/*
 * Copyright (c) 2016 Qiscus.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.qiscus.sdk.data.local;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.text.TextUtils;

import com.qiscus.sdk.util.QiscusLogger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

class QiscusDbOpenHelper extends SQLiteOpenHelper {

    private Context context;

    QiscusDbOpenHelper(Context context) {
        super(context, QiscusDb.DATABASE_NAME, null, QiscusDb.DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.beginTransaction();
        try {
            db.execSQL(QiscusDb.RoomTable.CREATE);
            db.execSQL(QiscusDb.MemberTable.CREATE);
            db.execSQL(QiscusDb.RoomMemberTable.CREATE);
            db.execSQL(QiscusDb.CommentTable.CREATE);
            db.execSQL(QiscusDb.FilesTable.CREATE);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        QiscusLogger.print("Upgrade database from : " + oldVersion + " to : " + newVersion);

        /* Upgrade DB using SQL scripts place at assets directory
         * format filename : qiscus.db_from_{oldVersion}_to_{newVersion}.sql
         * example : qiscus.db_from_14_to_15.sql
         */
        try {
            for (int i = oldVersion; i < newVersion; i++) {
                String migrationName = String.format("qiscus.db_from_%d_to_%d.sql", i, (i + 1));
                QiscusLogger.print("Looking for migration file : " + migrationName);
                readAndExecSQL(db, context, migrationName);
            }

        } catch (Exception e) {
            QiscusLogger.print("Exception running upgrade scripts : " + e.getMessage());
        }
    }

    private void readAndExecSQL(SQLiteDatabase db, Context context, String migrationName) {
        if (TextUtils.isEmpty(migrationName)) {
            QiscusLogger.print("SQL Script migration name is empty...");
            return;
        }

        QiscusLogger.print("SQL Script found...");
        AssetManager assets = context.getAssets();
        BufferedReader reader = null;

        try {
            InputStream inputStream = assets.open(migrationName);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            reader = new BufferedReader(inputStreamReader);
            execSQLScript(db, reader);
        } catch (IOException e) {
            QiscusLogger.print("Failed read SQL Script : " + e.getMessage());
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    QiscusLogger.print("Failed close reader : " + e.getMessage());
                }
            }
        }
    }

    private void execSQLScript(SQLiteDatabase db, BufferedReader reader) throws IOException {
        db.beginTransaction();
        try {
            String line;
            StringBuilder statement = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                statement.append(line);
                statement.append(System.getProperty("line.separator"));
                if (line.endsWith(";")) {
                    db.execSQL(statement.toString());
                    statement = new StringBuilder();
                }
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }

    }
}

@@ -1906,16 +1906,187 @@
 sion);%0A%0A
+        // Before version 14, we just clear old data%0A        if (oldVersion %3C 14) %7B%0A            clearOldData(db);%0A            onCreate(db);%0A            return;%0A        %7D%0A%0A
         
@@ -4473,21 +4473,646 @@
 ;%0A        %7D%0A
+    %7D%0A%0A    private void clearOldData(SQLiteDatabase db) %7B%0A        db.beginTransaction();%0A        try %7B%0A            db.execSQL(%22DROP TABLE IF EXISTS %22 + QiscusDb.RoomTable.TABLE_NAME);%0A            db.execSQL(%22DROP TABLE IF EXISTS %22 + QiscusDb.MemberTable.TABLE_NAME);%0A            db.execSQL(%22DROP TABLE IF EXISTS %22 + QiscusDb.RoomMemberTable.TABLE_NAME);%0A            db.execSQL(%22DROP TABLE IF EXISTS %22 + QiscusDb.CommentTable.TABLE_NAME);%0A            db.execSQL(%22DROP TABLE IF EXISTS %22 + QiscusDb.FilesTable.TABLE_NAME);%0A            db.setTransactionSuccessful();%0A        %7D finally %7B%0A            db.endTransaction();%0A        %7D
 %0A    %7D%0A%7D%0A

Handle data migration for database version lower than 14