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.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 = 6;
    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 String TABLE_LOGIN_USER_INFO = "login_user_info";//登录信息表
    static final String TABLE_LOGIN_USER_FUN_INFO = "login_user_fun_info";//权限表

    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), " +
                "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 " + SobotServiceInfoModel.ACCESS_TOKEN + " VARCHAR(700)";
                db.execSQL(sql);  // 登录表添加ACCESS_TOKEN
                String sql1 = "ALTER TABLE " + TABLE_LOGIN_USER
                        + " ADD COLUMN " + SobotServiceInfoModel.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;
            case 3:
                String sql4 = "ALTER TABLE " + TABLE_LOGIN_USER
                        + " ADD COLUMN login_account VARCHAR(40)";
                db.execSQL(sql4);  // 登录表添加登录账号
                String sql5 = "ALTER TABLE " + TABLE_LOGIN_USER
                        + " ADD COLUMN login_pwd VARCHAR(40)";
                db.execSQL(sql5);  // 登录表添加登录密码
                break;
            case 4:
                String sql6 = "ALTER TABLE " + TABLE_LOGIN_USER
                        + " ADD COLUMN wslinkDefault VARCHAR(40)";
                db.execSQL(sql6);  // 登录表添加通道信息2
                break;
            case 5:
                // 删除表的SQL语句
                String LOGIN_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_LOGIN_USER;
                db.execSQL(LOGIN_USER_TABLE);
                // 删除表的SQL语句
                String SERVICE_INFO_TABLE = "DROP TABLE IF EXISTS " + TABLE_SERVICE_INFO;
                db.execSQL(SERVICE_INFO_TABLE);
                String sql7 = "CREATE TABLE IF NOT EXISTS " + TABLE_LOGIN_USER_INFO + "(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                        "tempId VARCHAR(80)," +
                        "serviceNick VARCHAR(300)," +
                        "faceImg VARCHAR(300)," +
                        "maxAccept INTEGER," +
                        "blackFunction INTEGER DEFAULT 1," +
                        "transferFunction INTEGER DEFAULT 1, " +
                        "companyName VARCHAR(300), " +
                        "pu VARCHAR(40), " +
                        "puid VARCHAR(40), " +
                        "token VARCHAR(500), " +
                        "cusRoleName VARCHAR(200), " +
                        "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 , " +
                        "robotVersion INTEGER , " +
                        "onlineVersion INTEGER , " +
                        "serviceId VARCHAR(40)," +
                        "serviceNo VARCHAR(60)," +
                        "serviceName VARCHAR(600)," +
                        "newBossSwitch INTEGER," +
                        "region INTEGER," +
                        "serviceLanguage VARCHAR(12)," +
                        "phoneNo VARCHAR(20)," +
                        "globalPermissions VARCHAR(300)," +
                        "access_token VARCHAR(700)," +
                        "login_time INTEGER," +
                        "login_account VARCHAR(40)," +
                        "login_pwd VARCHAR(40)," +
                        "wslinkDefault VARCHAR(40)," +
                        "timezone VARCHAR(15)," +
                        "timezoneId VARCHAR(30)," +
                        "companyId VARCHAR(40))";
                db.execSQL(sql7);
                String sql8 = "CREATE TABLE IF NOT EXISTS " + TABLE_LOGIN_USER_FUN_INFO + "(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                        "id VARCHAR(20)," +
                        "checkedCode INTEGER," +
                        "code VARCHAR(40))";
                db.execSQL(sql8);
                break;
            default:
                break;
        }
    }

}
