package com.sobot.common.login.db;


import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.sobot.common.login.model.SobotServiceInfoModel;
import com.sobot.common.utils.SobotGlobalContext;
import com.sobot.gson.SobotGsonUtil;
import com.sobot.gson.reflect.TypeToken;
import com.sobot.utils.SobotStringUtils;

import java.util.List;

/**
 * 坐席信息 操作类
 */
public class ServiceInfoManager extends SobotCommonDao<SobotServiceInfoModel> {
    private ServiceInfoManager(Context context) {
        super(new SobotCommonDBHelper(context));
    }

    //volatile关键字确保多线程下正确处理
    private volatile static ServiceInfoManager mInstance;

    public static ServiceInfoManager getInstance() {
        //第一次判空
        if (mInstance == null) {
            //进入同步区域
            synchronized (ServiceInfoManager.class) {
                //第二次判空
                if (mInstance == null) {
                    mInstance = new ServiceInfoManager(SobotGlobalContext.getAppContext());
                }
            }
        }
        return mInstance;
    }

    @Override
    public String getTableName() {
        return SobotCommonDBHelper.TABLE_LOGIN_USER_INFO;
    }

    @Override
    public void unInit() {

    }

    @SuppressLint("Range")
    @Override
    public SobotServiceInfoModel parseCursorToBean(Cursor cursor) {
        SobotServiceInfoModel model = new SobotServiceInfoModel();
        model.setServiceNick(cursor.getString(cursor.getColumnIndex("nickName")));
        model.setMaxAccept(cursor.getInt(cursor.getColumnIndex("maxAccept")));
        model.setCompanyName(cursor.getString(cursor.getColumnIndex("companyName")));
        model.setPu(cursor.getString(cursor.getColumnIndex("pu")));
        model.setPuid(cursor.getString(cursor.getColumnIndex("puid")));
        model.setToken(cursor.getString(cursor.getColumnIndex("token")));
        model.setCusRoleName(cursor.getString(cursor.getColumnIndex("cusRoleName")));
        model.setCenterNumber(cursor.getString(cursor.getColumnIndex("centerNumber")));
        model.setAccountStatus(cursor.getString(cursor.getColumnIndex("accountStatus")));
        model.setImFlag(cursor.getInt(cursor.getColumnIndex("imFlag")));
        model.setCusRoleId(cursor.getInt(cursor.getColumnIndex("cusRoleId")));
        model.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
        model.setTopFlag(cursor.getInt(cursor.getColumnIndex("topFlag")));
        model.setSortFlag(cursor.getInt(cursor.getColumnIndex("sortFlag")));
        model.setFuseWork(cursor.getInt(cursor.getColumnIndex("fuseWork")));
        model.setCallV6Flag(cursor.getInt(cursor.getColumnIndex("callV6Flag")));
        model.setCallV1Flag(cursor.getInt(cursor.getColumnIndex("callV1Flag")));
        model.setTransferAuditFlag(cursor.getInt(cursor.getColumnIndex("transferAuditFlag")));
        model.setKbVersion(cursor.getInt(cursor.getColumnIndex("kbVersion")) + "");
        model.setRobotVersion(cursor.getInt(cursor.getColumnIndex("robotVersion")) + "");
        model.setOnlineVersion(cursor.getInt(cursor.getColumnIndex("onlineVersion")) + "");
        model.setCompanyId(cursor.getString(cursor.getColumnIndex("companyId")));
        model.setLogin_time(cursor.getLong(cursor.getColumnIndex(SobotServiceInfoModel.LOGIN_TIME)));
        model.setLoginAccount(cursor.getString(cursor.getColumnIndex("login_account")));
        model.setLoginPwd(cursor.getString(cursor.getColumnIndex("login_pwd")));
        model.setWslinkDefault(cursor.getString(cursor.getColumnIndex("wslinkDefault")));

        model.setServiceId(cursor.getString(cursor.getColumnIndex("serviceId")));
        model.setServiceNo(cursor.getString(cursor.getColumnIndex("serviceNo")));
        model.setServiceName(cursor.getString(cursor.getColumnIndex("serviceName")));
        model.setServiceWorkbenchLang(cursor.getString(cursor.getColumnIndex("serviceLanguage")));
        model.setNewBossSwitch(cursor.getInt(cursor.getColumnIndex("newBossSwitch")));
        model.setRegion(cursor.getInt(cursor.getColumnIndex("region")));
        model.setTimezone(cursor.getString(cursor.getColumnIndex("timezone")));
        model.setTimezoneId(cursor.getString(cursor.getColumnIndex("timezoneId")));
        model.setPhoneNo(cursor.getString(cursor.getColumnIndex("phoneNo")));
        String globalPermissions = cursor.getString(cursor.getColumnIndex("globalPermissions"));
        if (!SobotStringUtils.isEmpty(globalPermissions)) {
            List<String> globalPermissionslist = SobotGsonUtil.jsonToBeans(globalPermissions, new TypeToken<List<String>>() {
            }.getType());
            model.setGlobalPermissions(globalPermissionslist);
        }
        return model;
    }

    @Override
    public ContentValues getContentValues(SobotServiceInfoModel infoModel) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("_id", 8888);
        contentValues.put("serviceNick", infoModel.getServiceNick());
        contentValues.put("maxAccept", infoModel.getMaxAccept());
        contentValues.put("faceImg", infoModel.getFaceImg());
        contentValues.put("companyName", infoModel.getCompanyName());
        contentValues.put("pu", infoModel.getPu());
        contentValues.put("puid", infoModel.getPuid());
        contentValues.put("token", infoModel.getToken());
        contentValues.put("cusRoleName", infoModel.getCusRoleName());
        contentValues.put("centerNumber", infoModel.getCenterNumber());
        contentValues.put("accountStatus", infoModel.getAccountStatus());
        contentValues.put("imFlag", infoModel.isImFlag());
        contentValues.put("cusRoleId", infoModel.getCusRoleId());
        contentValues.put("status", infoModel.getStatus());
        contentValues.put("topFlag", infoModel.getTopFlag());
        contentValues.put("sortFlag", infoModel.getSortFlag());
        contentValues.put("fuseWork", infoModel.getFuseWork());
        contentValues.put("callV6Flag", infoModel.getCallV6Flag());
        contentValues.put("callV1Flag", infoModel.getCallV1Flag());
        contentValues.put("transferAuditFlag", infoModel.getTransferAuditFlag());
        int kbVersion = 1;
        if ("6".contains(SobotStringUtils.checkStringIsNull(infoModel.getKbVersion()))) {
            kbVersion = 6;
        }
        contentValues.put("kbVersion", kbVersion);
        contentValues.put("robotVersion", infoModel.getRobotVersion());
        contentValues.put("onlineVersion", infoModel.getOnlineVersion());
        contentValues.put("companyId", infoModel.getCompanyId());
        contentValues.put(SobotServiceInfoModel.LOGIN_TIME, infoModel.getLogin_time());
        contentValues.put("login_account", infoModel.getLoginAccount());
        contentValues.put("login_pwd", infoModel.getLoginPwd());
        contentValues.put("wslinkDefault", infoModel.getWslinkDefault());

        contentValues.put("serviceId", infoModel.getServiceId());
        contentValues.put("serviceNo", infoModel.getServiceNo());
        contentValues.put("serviceName", infoModel.getServiceName());
        contentValues.put("serviceLanguage", infoModel.getServiceWorkbenchLang());
        contentValues.put("newBossSwitch", infoModel.getNewBossSwitch());
        contentValues.put("region", infoModel.getRegion());
        contentValues.put("timezone", infoModel.getTimezone());
        contentValues.put("timezoneId", infoModel.getTimezoneId());
        contentValues.put("phoneNo", infoModel.getPhoneNo());
        if (!SobotStringUtils.isEmpty(infoModel.getGlobalPermissions())) {
            contentValues.put("globalPermissions", SobotGsonUtil.beanToJson(infoModel.getGlobalPermissions()));
        }
        return contentValues;
    }

    public void saveInfo(SobotServiceInfoModel host) {
        deleteAll();
        insert(host);
    }

    /**
     * 获取最近用户记录 按日期倒序
     */
    public SobotServiceInfoModel getInfo() {
        List<SobotServiceInfoModel> list = query(null, null, null, null, null, null, null);
        return list.size() > 0 ? list.get(0) : null;
    }
}
