package com.kfzs.duanduan.data.graph.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.data.graph.provider.base.BaseSQLiteOpenHelperCallbacks;
import com.kfzs.duanduan.data.graph.provider.user.UserColumns;
import com.kfzs.duanduan.datashare.BuildConfig;

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

    public static final String DATABASE_FILE_NAME = "KFZSGraph.db";
    private static final int DATABASE_VERSION = 1;
    private static KFZSGraphSQLiteOpenHelper sInstance;
    private final Context mContext;
    private final BaseSQLiteOpenHelperCallbacks mOpenHelperCallbacks;

    public static final String SQL_CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS "
            + UserColumns.TABLE_NAME + " ( "
            + UserColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + UserColumns.UID + " INTEGER NOT NULL, "
            + UserColumns.USER_NAME + " TEXT NOT NULL, "
            + UserColumns.UPDATE_TIME + " INTEGER NOT NULL, "
            + UserColumns.PHONE + " TEXT, "
            + UserColumns.NICKNAME + " TEXT, "
            + UserColumns.USER_ICON + " TEXT, "
            + UserColumns.AVATAR + " TEXT, "
            + UserColumns.OPEN_ID + " TEXT, "
            + UserColumns.MAIL + " TEXT, "
            + UserColumns.REFRESH_TOKEN + " TEXT "
            + ", CONSTRAINT unique_user UNIQUE (user_name) ON CONFLICT REPLACE"
            + " );";

    public static final String SQL_CREATE_INDEX_USER_UID = "CREATE INDEX IDX_USER_UID "
            + " ON " + UserColumns.TABLE_NAME + " ( " + UserColumns.UID + " );";

    public static final String SQL_CREATE_INDEX_USER_USER_NAME = "CREATE INDEX IDX_USER_USER_NAME "
            + " ON " + UserColumns.TABLE_NAME + " ( " + UserColumns.USER_NAME + " );";


    public static KFZSGraphSQLiteOpenHelper 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 KFZSGraphSQLiteOpenHelper newInstance(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            return newInstancePreHoneycomb(context);
        }
        return newInstancePostHoneycomb(context);
    }


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

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


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

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


    @Override
    public void onCreate(SQLiteDatabase db) {
        if (BuildConfig.DEBUG)
            Log.d(TAG, "onCreate");
        mOpenHelperCallbacks.onPreCreate(mContext, db);
        db.execSQL(SQL_CREATE_TABLE_USER);
        db.execSQL(SQL_CREATE_INDEX_USER_UID);
        db.execSQL(SQL_CREATE_INDEX_USER_USER_NAME);
        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);
    }
}
