/*
 * Copyright (c) 2015 LingoChamp Inc.
 *
 * 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.liulishuo.filedownloader.services;

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

import com.liulishuo.filedownloader.model.ConnectionModel;
import com.liulishuo.filedownloader.model.FileDownloadModel;


/**
 * The default opener of the filedownloader database helper.
 */
class DefaultDatabaseOpenHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "filedownloader.db";
    private static final int DATABASE_VERSION = 3;

    public DefaultDatabaseOpenHelper(final Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE IF NOT EXISTS " +
                DefaultDatabaseImpl.TABLE_NAME + "( " +
                FileDownloadModel.ID + " INTEGER PRIMARY KEY, " + // id
                FileDownloadModel.URL + " VARCHAR, " + // url
                FileDownloadModel.PATH + " VARCHAR, " + // path
                FileDownloadModel.STATUS + " TINYINT(7), " + // status ,ps SQLite will auto change to integer.
                FileDownloadModel.SOFAR + " INTEGER, " +// so far
                FileDownloadModel.TOTAL + " INTEGER, " +// total
                FileDownloadModel.ERR_MSG + " VARCHAR, " + // error message
                FileDownloadModel.ETAG + " VARCHAR, " +// e tag
                FileDownloadModel.PATH_AS_DIRECTORY + " TINYINT(1) DEFAULT 0, " +// path as directory
                FileDownloadModel.FILENAME + " VARCHAR, " +// path as directory
                FileDownloadModel.CONNECTION_COUNT + " INTEGER DEFAULT 1" + // connection count
                ")");
        db.execSQL("CREATE TABLE IF NOT EXISTS " +
                DefaultDatabaseImpl.CONNECTION_TABLE_NAME + "( " +
                ConnectionModel.ID + " INTEGER, " +
                ConnectionModel.INDEX + " INTEGER, " +
                ConnectionModel.START_OFFSET + " INTEGER, " +
                ConnectionModel.CURRENT_OFFSET + " INTEGER, " +
                ConnectionModel.END_OFFSET + " INTEGER, " +
                "PRIMARY KEY ( " + ConnectionModel.ID + ", " + ConnectionModel.INDEX + " )" +
                ")");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        if (oldVersion < 2) {
            String addAsDirectoryColumn = "ALTER TABLE " + DefaultDatabaseImpl.TABLE_NAME +
                    " ADD COLUMN " + FileDownloadModel.PATH_AS_DIRECTORY +
                    " TINYINT(1) DEFAULT 0";
            db.execSQL(addAsDirectoryColumn);

            String addFilenameColumn = "ALTER TABLE " + DefaultDatabaseImpl.TABLE_NAME +
                    " ADD COLUMN " + FileDownloadModel.FILENAME +
                    " VARCHAR";
            db.execSQL(addFilenameColumn);
        }

        if (oldVersion < 3) {
            final String addConnectionCount = "ALTER TABLE " + DefaultDatabaseImpl.TABLE_NAME +
                    " ADD COLUMN " + FileDownloadModel.CONNECTION_COUNT +
                    " INTEGER DEFAULT 1";
            db.execSQL(addConnectionCount);

            db.execSQL("CREATE TABLE IF NOT EXISTS " +
                    DefaultDatabaseImpl.CONNECTION_TABLE_NAME + "( " +
                    ConnectionModel.ID + " INTEGER, " +
                    ConnectionModel.INDEX + " INTEGER, " +
                    ConnectionModel.START_OFFSET + " INTEGER, " +
                    ConnectionModel.CURRENT_OFFSET + " INTEGER, " +
                    ConnectionModel.END_OFFSET + " INTEGER, " +
                    "PRIMARY KEY ( " + ConnectionModel.ID + ", " + ConnectionModel.INDEX + " )" +
                    ")");
        }
    }
}

@@ -771,16 +771,41 @@
 nHelper;
+%0Aimport android.os.Build;
 %0A%0Aimport
@@ -1294,16 +1294,483 @@
 RSION);%0A
+        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN) %7B%0A            setWriteAheadLoggingEnabled(true);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void onOpen(SQLiteDatabase db) %7B%0A        super.onOpen(db);%0A%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN) %7B%0A            setWriteAheadLoggingEnabled(true);%0A        %7D else if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.HONEYCOMB) %7B%0A            db.enableWriteAheadLogging();%0A        %7D%0A
     %7D%0A%0A 

enable the WAL for the the default sqlite to speed up sql-operation because our case is concurrently accessed and modified by multiple threads at the same time