package com.sobot.network.http.db;

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

import com.sobot.network.apiUtils.SobotHttpGlobalContext;
import com.sobot.network.http.model.SobotProgress;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * DB辅助类
 */
class SobotDBHelper extends SQLiteOpenHelper {

    private static final String DB_CACHE_NAME = "sobot.db";
    private static final int DB_CACHE_VERSION = 1;
    static final String TABLE_FILECACHE = "fileCache";

    static final Lock lock = new ReentrantLock();

    private StTableEntity fileCacheTableEntity = new StTableEntity(TABLE_FILECACHE);

    SobotDBHelper() {
        this(SobotHttpGlobalContext.getAppContext());
    }

    SobotDBHelper(Context context) {
        super(context, DB_CACHE_NAME, null, DB_CACHE_VERSION);

        fileCacheTableEntity.addColumn(new StColumnEntity(SobotProgress.TAG, "VARCHAR", true, true))//
                .addColumn(new StColumnEntity(SobotProgress.URL, "VARCHAR"))//
                .addColumn(new StColumnEntity(SobotProgress.IS_UPLOAD, "INTEGER"))//
                .addColumn(new StColumnEntity(SobotProgress.FOLDER, "VARCHAR"))//
                .addColumn(new StColumnEntity(SobotProgress.FILE_PATH, "VARCHAR"))//
                .addColumn(new StColumnEntity(SobotProgress.FILE_NAME, "VARCHAR"))//
                .addColumn(new StColumnEntity(SobotProgress.FRACTION, "VARCHAR"))//
                .addColumn(new StColumnEntity(SobotProgress.TOTAL_SIZE, "INTEGER"))//
                .addColumn(new StColumnEntity(SobotProgress.CURRENT_SIZE, "INTEGER"))//
                .addColumn(new StColumnEntity(SobotProgress.STATUS, "INTEGER"))//
                .addColumn(new StColumnEntity(SobotProgress.PRIORITY, "INTEGER"))//
                .addColumn(new StColumnEntity(SobotProgress.DATE, "INTEGER"));
//                .addColumn(new StColumnEntity(SobotProgress.REQUEST, "BLOB"));
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        onFirstCreate(db);
        // 若不是第一个版本安装，直接执行数据库升级
        // 请不要修改FIRST_DATABASE_VERSION的值，其为第一个数据库版本大小
        final int FIRST_DATABASE_VERSION = 1;
        onUpgrade(db, FIRST_DATABASE_VERSION, DB_CACHE_VERSION);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.beginTransaction();
        try {
            while (oldVersion < newVersion) {
                upgradeDB(db, oldVersion, newVersion);
                oldVersion++;
            }
            db.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();
        }
    }

    /**
     * 初始版本创建数据库需要执行的操作
     *
     * @param db
     */
    private void onFirstCreate(SQLiteDatabase db) {
        db.execSQL(fileCacheTableEntity.buildTableString());
    }

    /**
     * 升级数据需要执行的操作，初始版本以后所有版本的升级都会执行这个方法
     *
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    private void upgradeDB(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (oldVersion) {
            case 1:
//                升级数据库   1 => 2
//                db.execSQL(Sql.CreateTable.CREATE_CATEGORY);
                break;
            case 2:
//                升级数据库   2 => 3
//                db.execSQL("ALTER TABLE category ADD COLUMN created_at TEXT");
                break;
            default:
                break;
        }

    }
}
