package com.sobot.common.login.db;

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

import com.sobot.common.login.model.LoginUserEntity;
import com.sobot.common.login.model.SobotServiceInfoModel;

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

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

    private static final String DB_CACHE_NAME = "sobot_common.db";
    //    private static final int DB_CACHE_VERSION = 1;
    private static final int DB_CACHE_VERSION = 3;
    static final String TABLE_LOGIN_USER = "login_user";
    static final String TABLE_SOBOT_HOST = "sobot_host";
    static final String TABLE_SERVICE_INFO = "sobot_service";

    static final Lock lock = new ReentrantLock();

    SobotCommonDBHelper(Context context) {
        super(context, DB_CACHE_NAME, null, DB_CACHE_VERSION);
    }

    @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) {
        String sql = "create table " + TABLE_LOGIN_USER + "(_id INTEGER PRIMARY KEY," +
                "tempId varchar(80)," +
                "nickName varchar(300)," +
                "staffName varchar(600), " +
                "maxServiceCount INTEGER," +
                "blackFunction INTEGER DEFAULT 1," +
                "ticketFunction varchar(1000), " +
                "transferFunction INTEGER DEFAULT 1, " +
                "face varchar(300), " +
                "companyName varchar(300), " +
                "appKey varchar(40), " +
                "sid varchar(40), " +
                "pu varchar(40), " +
                "puid varchar(40), " +
                "token varchar(500), " +
                "cusRoleName varchar(200), " +
                "aid varchar(40), " +
                "centerNumber varchar(40), " +
                "accountStatus varchar(2), " +
                "imFlag INTEGER, " +
                "cusRoleId INTEGER, " +
                "status INTEGER DEFAULT 1, " +
                "topFlag INTEGER, " +
                "sortFlag INTEGER , " +
                "fuseWork INTEGER , " +
                "callV6Flag INTEGER , " +
                "callV1Flag INTEGER , " +
                "transferAuditFlag INTEGER , " +
                "kbVersion INTEGER , " +
                "local_language varchar(10) , " +
                "companyId varchar(40))";
        db.execSQL(sql);

        //添加客服信息表
        String sql_service = "create table " + TABLE_SERVICE_INFO + "(" +
                "serviceId varchar(40)," +
                "serviceName varchar(600)," +
                "cusRoleId varchar(10)," +
                "cusRoleName varchar(200)," +
                "functionStr varchar(1000)," +
                "newBossSwitch INTEGER," +
                "region INTEGER," +
                "serviceLanguage varchar(12)," +
                "companyId varchar(40)," +
                "callV6Flag INTEGER," +
                "phoneNo varchar(20)," +
                "globalPermissions varchar(300)," +
                "language varchar(10))";
        db.execSQL(sql_service);

        String sql_host = "create table " + TABLE_SOBOT_HOST + "(" +
                "host varchar(80)," +
                "hostDesc varchar(80)," +
                "janussipProxy varchar(80)," +
                "openApiHost varchar(80)," +
                "stompSocket varchar(80)," +
                "janusSocket varchar(80))";
        db.execSQL(sql_host);
    }

    /**
     * 升级数据需要执行的操作，初始版本以后所有版本的升级都会执行这个方法
     *
     * @param db
     * @param oldVersion
     * @param newVersion
     */
    private void upgradeDB(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (oldVersion) {
            case 1:
                String sql = "ALTER TABLE " + TABLE_LOGIN_USER
                        + " ADD COLUMN " + LoginUserEntity.ACCESS_TOKEN + " VARCHAR(700)";
                db.execSQL(sql);  // 执行修改表，添加字段的逻辑。
                String sql1 = "ALTER TABLE " + TABLE_LOGIN_USER
                        + " ADD COLUMN " + LoginUserEntity.LOGIN_TIME + " INTEGER";
                db.execSQL(sql1);  // 执行修改表，添加字段的逻辑。
                break;
            case 2:
                String sql2 = "ALTER TABLE " + TABLE_SERVICE_INFO
                        + " ADD COLUMN " + SobotServiceInfoModel.TIMEZONE + " VARCHAR(15)";
                db.execSQL(sql2);  // 执行修改表，添加字段的逻辑。
                String sql3 = "ALTER TABLE " + TABLE_SERVICE_INFO
                        + " ADD COLUMN " + SobotServiceInfoModel.TIMEZONEID + " VARCHAR(30)";
                db.execSQL(sql3);  // 执行修改表，添加字段的逻辑。
                break;
            default:
                break;
        }
    }

}
