package com.kfzs.duanduan.datashare.provider;

// @formatter:off

import android.annotation.TargetApi;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.DefaultDatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.util.Log;

import com.kfzs.duanduan.datashare.BuildConfig;
import com.kfzs.duanduan.datashare.KFZSDDSQLiteOpenHelperCallbacks;
import com.kfzs.duanduan.datashare.provider.base.BaseSQLiteOpenHelperCallbacks;
import com.kfzs.duanduan.datashare.provider.downloadtask.DownloadtaskColumns;
import com.kfzs.duanduan.datashare.provider.records.RecordsColumns;
import com.kfzs.duanduan.datashare.provider.session.SessionColumns;

public class KFZSDDSQLiteOpenHelper extends SQLiteOpenHelper {
    private static final String TAG = KFZSDDSQLiteOpenHelper.class.getSimpleName();

    public static final String DATABASE_FILE_NAME = "kfzsdd.db";
    private static final int DATABASE_VERSION = 3;
    private static KFZSDDSQLiteOpenHelper sInstance;
    private final Context mContext;
    private final BaseSQLiteOpenHelperCallbacks mOpenHelperCallbacks;

    public static final String SQL_CREATE_TABLE_DOWNLOADTASK = "CREATE TABLE IF NOT EXISTS "
            + DownloadtaskColumns.TABLE_NAME + " ( "
            + DownloadtaskColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + DownloadtaskColumns.CREATE_TIME + " INTEGER NOT NULL, "
            + DownloadtaskColumns.DOWNLOAD_TASK_ID + " INTEGER, "
            + DownloadtaskColumns.GAME_NAME + " TEXT NOT NULL, "
            + DownloadtaskColumns.GAME_ID + " INTEGER NOT NULL, "
            + DownloadtaskColumns.ICON_URL + " TEXT NOT NULL, "
            + DownloadtaskColumns.PERCENT + " INTEGER, "
            + DownloadtaskColumns.DOWNLOADED_SIZE + " REAL, "
            + DownloadtaskColumns.TOTAL_SIZE + " REAL, "
            + DownloadtaskColumns.STATUS + " INTEGER NOT NULL, "
            + DownloadtaskColumns.DOWNLOAD_URL + " TEXT NOT NULL, "
            + DownloadtaskColumns.PACKAGE_NAME + " TEXT NOT NULL, "
            + DownloadtaskColumns.VERSION_CODE + " INTEGER NOT NULL, "
            + DownloadtaskColumns.APK_PATH + " TEXT NOT NULL "
            + ", CONSTRAINT unique_download_task UNIQUE (download_url) ON CONFLICT REPLACE"
            + " );";

    public static final String SQL_CREATE_INDEX_DOWNLOADTASK_CREATE_TIME = "CREATE INDEX IDX_DOWNLOADTASK_CREATE_TIME "
            + " ON " + DownloadtaskColumns.TABLE_NAME + " ( " + DownloadtaskColumns.CREATE_TIME + " );";

    public static final String SQL_CREATE_TABLE_RECORDS = "CREATE TABLE IF NOT EXISTS "
            + RecordsColumns.TABLE_NAME + " ( "
            + RecordsColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + RecordsColumns.CREATE_TIME + " INTEGER NOT NULL, "
            + RecordsColumns.NAME + " TEXT NOT NULL "
            + ", CONSTRAINT unique_records UNIQUE (create_time) ON CONFLICT REPLACE"
            + " );";

    public static final String SQL_CREATE_INDEX_RECORDS_CREATE_TIME = "CREATE INDEX IDX_RECORDS_CREATE_TIME "
            + " ON " + RecordsColumns.TABLE_NAME + " ( " + RecordsColumns.CREATE_TIME + " );";

    public static final String SQL_CREATE_TABLE_SESSION = "CREATE TABLE IF NOT EXISTS "
            + SessionColumns.TABLE_NAME + " ( "
            + SessionColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + SessionColumns.ACCOUNT + " TEXT NOT NULL, "
            + SessionColumns.OPEN_ID + " TEXT, "
            + SessionColumns.UPDATE_TIME + " INTEGER NOT NULL, "
            + SessionColumns.TOKEN + " TEXT, "
            + SessionColumns.UID + " INTEGER, "
            + SessionColumns.HASH + " INTEGER "
            + ", CONSTRAINT unique_session UNIQUE (account) ON CONFLICT REPLACE"
            + " );";

    public static final String SQL_CREATE_INDEX_SESSION_ACCOUNT = "CREATE INDEX IDX_SESSION_ACCOUNT "
            + " ON " + SessionColumns.TABLE_NAME + " ( " + SessionColumns.ACCOUNT + " );";

    public static final String SQL_CREATE_INDEX_SESSION_OPEN_ID = "CREATE INDEX IDX_SESSION_OPEN_ID "
            + " ON " + SessionColumns.TABLE_NAME + " ( " + SessionColumns.OPEN_ID + " );";


    public static KFZSDDSQLiteOpenHelper getInstance(Context context) {
        // Use the application context, which will ensure that you
        // don't accidentally leak an Activity's context.
        // See this article for more information: http://bit.ly/6LRzfx
        if (sInstance == null) {
            sInstance = newInstance(context.getApplicationContext());
        }
        return sInstance;
    }

    private static KFZSDDSQLiteOpenHelper newInstance(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return newInstancePreHoneycomb(context);
        }
        return newInstancePostHoneycomb(context);
    }


    /*
     * Pre Honeycomb.
     */
    private static KFZSDDSQLiteOpenHelper newInstancePreHoneycomb(Context context) {
        return new KFZSDDSQLiteOpenHelper(context);
    }

    private KFZSDDSQLiteOpenHelper(Context context) {
        super(context, DATABASE_FILE_NAME, null, DATABASE_VERSION);
        mContext = context;
        mOpenHelperCallbacks = new KFZSDDSQLiteOpenHelperCallbacks();
    }


    /*
     * Post Honeycomb.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private static KFZSDDSQLiteOpenHelper newInstancePostHoneycomb(Context context) {
        return new KFZSDDSQLiteOpenHelper(context, new DefaultDatabaseErrorHandler());
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private KFZSDDSQLiteOpenHelper(Context context, DatabaseErrorHandler errorHandler) {
        super(context, DATABASE_FILE_NAME, null, DATABASE_VERSION, errorHandler);
        mContext = context;
        mOpenHelperCallbacks = new KFZSDDSQLiteOpenHelperCallbacks();
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        if (BuildConfig.DEBUG)
            Log.d(TAG, "onCreate");
        mOpenHelperCallbacks.onPreCreate(mContext, db);
        db.execSQL(SQL_CREATE_TABLE_DOWNLOADTASK);
        db.execSQL(SQL_CREATE_INDEX_DOWNLOADTASK_CREATE_TIME);
        db.execSQL(SQL_CREATE_TABLE_RECORDS);
        db.execSQL(SQL_CREATE_INDEX_RECORDS_CREATE_TIME);
        db.execSQL(SQL_CREATE_TABLE_SESSION);
        db.execSQL(SQL_CREATE_INDEX_SESSION_ACCOUNT);
        db.execSQL(SQL_CREATE_INDEX_SESSION_OPEN_ID);
        mOpenHelperCallbacks.onPostCreate(mContext, db);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        mOpenHelperCallbacks.onOpen(mContext, db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        mOpenHelperCallbacks.onUpgrade(mContext, db, oldVersion, newVersion);
    }
}
