package com.sobot.common.login.db;

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

import com.sobot.common.login.model.ServiceFunctionVoModel;
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 (LoginUserManager.class) {
                //第二次判空
                if (mInstance == null) {
                    mInstance = new ServiceInfoManager(SobotGlobalContext.getAppContext());
                }
            }
        }
        return mInstance;
    }

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

    @Override
    public void unInit() {

    }

    @Override
    public SobotServiceInfoModel parseCursorToBean(Cursor cursor) {
        SobotServiceInfoModel model = new SobotServiceInfoModel();
        model.setServiceId(cursor.getString(cursor.getColumnIndex("serviceId")));
        model.setServiceName(cursor.getString(cursor.getColumnIndex("serviceName")));
        model.setCusRoleId(cursor.getString(cursor.getColumnIndex("cusRoleId")));
        model.setCusRoleName(cursor.getString(cursor.getColumnIndex("cusRoleName")));
        model.setServiceLanguage(cursor.getString(cursor.getColumnIndex("serviceLanguage")));
        model.setNewBossSwitch(cursor.getInt(cursor.getColumnIndex("newBossSwitch")));
        model.setRegion(cursor.getInt(cursor.getColumnIndex("region")));
        String functionStr = cursor.getString(cursor.getColumnIndex("functionStr"));
        if (!SobotStringUtils.isEmpty(functionStr)) {
            List<ServiceFunctionVoModel> list = SobotGsonUtil.jsonToBeans(functionStr, new TypeToken<List<ServiceFunctionVoModel>>() {
            }.getType());
            model.setFunctionStr(list);
        }
        model.setCompanyId(cursor.getString(cursor.getColumnIndex("companyId")));
        model.setCallV6Flag(cursor.getInt(cursor.getColumnIndex("callV6Flag")));
        model.setLanguage(cursor.getString(cursor.getColumnIndex("language")));
        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("serviceId", infoModel.getServiceId());
        contentValues.put("serviceName", infoModel.getServiceName());
        contentValues.put("cusRoleId", infoModel.getCusRoleId());
        contentValues.put("cusRoleName", infoModel.getCusRoleName());
        contentValues.put("serviceLanguage", infoModel.getServiceLanguage());
        contentValues.put("newBossSwitch", infoModel.getNewBossSwitch());
        contentValues.put("region", infoModel.getRegion());
        if (!SobotStringUtils.isEmpty(infoModel.getFunctionStr())) {
            contentValues.put("functionStr", SobotGsonUtil.beanToJson(infoModel.getFunctionStr()));
        }
        contentValues.put("companyId", infoModel.getCompanyId());
        contentValues.put("callV6Flag", infoModel.getCallV6Flag());
        contentValues.put("language", infoModel.getLanguage());
        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;
    }
}
