package com.sobot.common.login.db;

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

import com.sobot.common.login.model.LoginUserEntity;
import com.sobot.common.utils.SobotGlobalContext;
import com.sobot.gson.SobotGsonUtil;
import com.sobot.utils.SobotStringUtils;

import java.util.List;
import java.util.Map;

/**
 * 登录用户信息 操作类
 */
public class LoginUserManager extends SobotCommonDao<LoginUserEntity> {
    private LoginUserManager(Context context) {
        super(new SobotCommonDBHelper(context));
    }

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

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

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

    @Override
    public void unInit() {

    }

    @Override
    public LoginUserEntity parseCursorToBean(Cursor cursor) {
        LoginUserEntity model = new LoginUserEntity();
        model.setTempId(cursor.getString(cursor.getColumnIndex("tempId")));
        model.setNickName(cursor.getString(cursor.getColumnIndex("nickName")));
        model.setStaffName(cursor.getString(cursor.getColumnIndex("staffName")));
        model.setMaxServiceCount(cursor.getInt(cursor.getColumnIndex("maxServiceCount")));
        model.setBlackFunction(cursor.getInt(cursor.getColumnIndex("blackFunction")));
        model.setTransferFunction(cursor.getInt(cursor.getColumnIndex("transferFunction")));
        String ticketFunction = cursor.getString(cursor.getColumnIndex("ticketFunction"));
        if (!SobotStringUtils.isEmpty(ticketFunction)){
            Map<String, Boolean> map = SobotGsonUtil.jsonToMaps(ticketFunction);
            model.setTicketFunction(map);
        }
        model.setFace(cursor.getString(cursor.getColumnIndex("face")));
        model.setCompanyName(cursor.getString(cursor.getColumnIndex("companyName")));
        model.setAppKey(cursor.getString(cursor.getColumnIndex("appKey")));
        model.setSid(cursor.getString(cursor.getColumnIndex("sid")));
        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.setAid(cursor.getString(cursor.getColumnIndex("aid")));
        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.setCompanyId(cursor.getString(cursor.getColumnIndex("companyId")));
        model.setLanguage(cursor.getString(cursor.getColumnIndex("local_language")));
        model.setAccess_token(cursor.getString(cursor.getColumnIndex(LoginUserEntity.ACCESS_TOKEN)));
        model.setLogin_time(cursor.getLong(cursor.getColumnIndex(LoginUserEntity.LOGIN_TIME)));
        return model;
    }

    @Override
    public ContentValues getContentValues(LoginUserEntity userAccount) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("_id", 8888);
        contentValues.put("tempId", userAccount.getTempId());
        contentValues.put("nickName", userAccount.getNickName());
        contentValues.put("staffName", userAccount.getStaffName());
        contentValues.put("maxServiceCount", userAccount.getMaxServiceCount());
        contentValues.put("blackFunction", userAccount.getBlackFunction());
        contentValues.put("transferFunction", userAccount.getTransferFunction());
        if (userAccount.getTicketFunction() != null) {
            contentValues.put("ticketFunction", SobotGsonUtil.beanToJson(userAccount.getTicketFunction()));
        }
        contentValues.put("face", userAccount.getFace());
        contentValues.put("companyName", userAccount.getCompanyName());
        contentValues.put("appKey", userAccount.getAppKey());
        contentValues.put("sid", userAccount.getSid());
        contentValues.put("pu", userAccount.getPu());
        contentValues.put("puid", userAccount.getPuid());
        contentValues.put("token", userAccount.getToken());
        contentValues.put("cusRoleName", userAccount.getCusRoleName());
        contentValues.put("aid", userAccount.getAid());
        contentValues.put("centerNumber", userAccount.getCenterNumber());
        contentValues.put("accountStatus", userAccount.getAccountStatus());
        contentValues.put("imFlag", userAccount.isImFlag());
        contentValues.put("cusRoleId", userAccount.getCusRoleId());
        contentValues.put("status", userAccount.getStatus());
        contentValues.put("topFlag", userAccount.getTopFlag());
        contentValues.put("sortFlag", userAccount.getSortFlag());
        contentValues.put("fuseWork", userAccount.getFuseWork());
        contentValues.put("callV6Flag", userAccount.getCallV6Flag());
        contentValues.put("callV1Flag", userAccount.getCallV1Flag());
        contentValues.put("transferAuditFlag", userAccount.getTransferAuditFlag());
        int kbVersion = 1;
        if ("6".contains(SobotStringUtils.checkStringIsNull(userAccount.getKbVersion()))) {
            kbVersion = 6;
        }
        contentValues.put("kbVersion", kbVersion);
        contentValues.put("companyId", userAccount.getCompanyId());
        contentValues.put("local_language", userAccount.getLanguage());
        contentValues.put(LoginUserEntity.ACCESS_TOKEN, userAccount.getAccess_token());
        contentValues.put(LoginUserEntity.LOGIN_TIME, userAccount.getLogin_time());
        return contentValues;
    }

    public void saveUser(LoginUserEntity user) {
        insert(user);
    }

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