package com.sobot.common.login.permission;

import android.text.TextUtils;

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

/**
 * 权限管理类
 */

public class SobotPermissionManager {

    public static final int ONLINE_CUSTOMER_SERVICE = 1111;//在线客服
    public static final int ONLINE_ADMINISTRATORS = 2222;//在线管理员
    public static final int SUPER_ADMINISTRATORS = 3333;//超级管理员
    public static final int ALL_FUNCTION_ADMINISTRATORS = 4444;//全功能管理员
    public static final int WORK_ORDER_CUSTOMER_SERVICE = 5555;//工单客服
    public static final int CALL_CUSTOMER_SERVICE = 6669;//呼叫客服
    private static final int ALL_FUNCTION_CUSTOMER_SERVICE = 7777;//全功能客服
    public static final int WORK_ORDER_ADMINISTRATORS = 8888;//工单管理员
    public static final int CALL_ADMINISTRATORS = 9999;//呼叫管理员
    private static SobotPermissionApi instance;

    private static SobotPermissionApi getInstance() {
        if (instance == null) {
            if (SobotLoginTools.getInstance().getLoginUser() != null) {
                switch (SobotLoginTools.getInstance().getLoginUser().getCusRoleId()) {
                    case ONLINE_CUSTOMER_SERVICE://在线客服
                        instance = new PermissionOnLineCustomerService();
                        break;
                    case ONLINE_ADMINISTRATORS://在线管理员
                        instance = new PermissionOnLineAdministrators();
                        break;
                    case SUPER_ADMINISTRATORS://超级管理员
                        instance = new PermissionSuper();
                        break;
                    case ALL_FUNCTION_ADMINISTRATORS://全功能管理员
                        instance = new PermissionAllFunctionAdministrator();
                        break;
                    case WORK_ORDER_CUSTOMER_SERVICE://工单客服
                        instance = new PermissionWorkOrderCustomerService();
                        break;
                    case CALL_CUSTOMER_SERVICE://呼叫客服
                        instance = new PermissionCallCustomerService();
                        break;
                    case ALL_FUNCTION_CUSTOMER_SERVICE://全功能客服
                        instance = new PermissionAllFunctionCustomerService();
                        break;
                    case WORK_ORDER_ADMINISTRATORS://工单管理员
                        instance = new PermissionWorkOrderAdministrators();
                        break;
                    case CALL_ADMINISTRATORS://呼叫管理员
                        instance = new PermissionCallAdministrators();
                        break;
                }
            }
        }
        return instance;
    }

    public static boolean checkPermission(int type) {
        SobotServiceInfoModel serviceInfoModel = SobotLoginTools.getInstance().getServiceInfo();
        if (serviceInfoModel != null) {
            if (serviceInfoModel.getNewBossSwitch() > 0) {
                return isHasPermission(type + "");
            } else {
                SobotPermissionApi instance = getInstance();
                if (instance != null) {
                    return instance.checkPermission(type);
                }
            }
        } else {
            SobotPermissionApi instance = getInstance();
            if (instance != null) {
                return instance.checkPermission(type);
            }
        }
        return false;
    }

    public static void clearInstance() {
        instance = null;
    }


    //-------2.8.6添加----------------
    public static final String USER_PERMISSION_TYPE_DEL_WORK_ORDER = "1222";// 可删除工单
    public static final String USER_PERMISSION_TYPE_DEL_WORK_ORDER_REPLY = "1223";// 可删除工单回复
    public static final String USER_PERMISSION_TYPE_COLSE_WORK_ORDER = "1224";// 可关闭工单
    public static final String USER_PERMISSION_TYPE_ACTIVATION_WORK_ORDER = "1225";// 可激活工单
    public static final String USER_PERMISSION_TYPE_WORK_SEARCH = "1227";// 工单全局搜索
    public static final String USER_PERMISSION_TYPE_WORK_HISTORY = "1230";// 工单操作历史权限

    public static final String USER_PERMISSION_TYPE_WORK_STATUS = "1231";// 工单状态 可见/不可见


    //会话
    public static final String MODULE_CHAT = "module_chat";
    //工单中心
    public static final String MODULE_ORDER = "module_order";
    //工单编辑
    public static final String MODULE_ORDER_EDIT = "module_order_edit";
    //删除工单
    public static final String MODULE_ORDER_DELETE = "module_order_delete";
    //删除回复
    public static final String MODULE_ORDER_REPLYDELETE = "module_order_replydelete";
    //关闭工单
    public static final String MODULE_ORDER_CLOSE = "module_order_close";
    //激活工单
    public static final String MODULE_ORDER_ACTIVATION = "module_order_activation";
    //工单全局搜索
    public static final String MODULE_ORDER_SEARCH = "module_order_search";
    //工单操作记录
    public static final String MODULE_ORDER_OPERATE = "module_order_operate";
    //工单状态
    public static final String MODULE_ORDER_STATUS = "module_order_status";
    //客户
    public static final String MODULE_USERS = "module_users";
    //呼叫
    public static final String MODULE_CALL = "module_call";
    //监控统计
    public static final String MODULE_MONITORINGSTATISTICS = "module_monitoringstatistics";
    //在线统计
    public static final String MODULE_STATISTICS_ONLINE = "module_statistics_online";
    //呼叫统计
    public static final String MODULE_STATISTICS_CALL = "module_statistics_call";
    //呼叫监控
    public static final String MODULE_MONITORING_CALL = "module_monitoring_call";
    //在线监控
    public static final String MODULE_MONITORING_ONLINE = "module_monitoring_online";
    //工单统计
    public static final String MODULE_STATISTICS_ORDER = "module_statistics_order";
    //机器人统计
    public static final String MODULE_STATISTICS_ROBOT = "module_statistics_robot";
    //个人状态
    public static final String MODULE_SETTING_STATUS = "module_setting_status";
    //会话顺序
    public static final String MODULE_SETTING_PLAIN = "module_setting_plain";

    //-----------------------------------呼叫权限----------------------------------------
    // 通话记录
    public static final String CCCV6_THJL = "cccv6-thjl";
    // 通话详情
    public static final String CCCV6_THXQ = "cccv6-thxq";
    // 通话信息
    public static final String CCCV6_THXQ_THXX = "cccv6-thxq-thxx";
    // 服务总结
    public static final String CCCV6_THXQ_FWZJ = "cccv6-thxq-fwzj";
    // 客户信息
    public static final String CCCV6_THXQ_KHXX = "cccv6-thxq-khxx";
    // 业务记录
    public static final String CCCV6_THXQ_YWJL = "cccv6-thxq-ywjl";
    // 录音播放
    public static final String CCCV6_THXQ_LYBF = "cccv6-thxq-lybf";
    // 录音下载
    public static final String CCCV6_THXQ_LYXZ = "cccv6-thxq-lyxz";
    // 保存服务总结
    public static final String CCCV6_THXQ_BCFWZJ = "cccv6-thxq-bcfwzj";
    // 新增客户
    public static final String CCCV6_THXQ_XZKH = "cccv6-thxq-xzkh";
    // 关联客户
    public static final String CCCV6_THXQ_GLKH = "cccv6-thxq-glkh";
    // 编辑客户
    public static final String CCCV6_THXQ_BJKH = "cccv6-thxq-bjkh";
    // 电话回拨
    public static final String CCCV6_THXQ_DHHB = "cccv6-thxq-dhhb";
    // 电话条
    public static final String CCCV6_DHT = "app-cccv6-dht-dht";
    // 修改绑定电话号码
    public static final String CCCV6_DHT_XGBDDHHM = "app-cccv6-dht-xgdddh";
    // 修改分机账号
    public static final String CCCV6_DHT_XGFJZH = "app-cccv6-dht-xgfjzh";
    // 离线后自动解绑
    public static final String CCCV6_DHT_LXHZDJB = "app-cccv6-dht-lxhzdjb";
    // 转移
    public static final String CCCV6_DHT_ZY = "cccv6-dht-zy";
    // 咨询
    public static final String CCCV6_DHT_ZX = "cccv6-dht-zx";
    // 保持和保持取消
    public static final String CCCV6_DHT_BCHBCQX = "app-cccv6-dht-baoc";
    // 静音和静音取消
    public static final String CCCV6_DHT_JYHJYQX = "app-cccv6-dht-jingy";
    // 发送满意度
    public static final String CCCV6_DHT_FSMYD = "app-cccv6-dht-fsmyd";
    // 延长整理时长
    public static final String CCCV6_DHT_YCZLSC = "app-cccv6-dht-yczlsc";
    // 内部通话
    public static final String CCCV6_DHT_INNER = "cccv6-dht-";
    // 结束整理并置忙
    public static final String CCCV6_DHT_JSZLBZM = "app-cccv6-dht-jszlbzm";
    // 重置离线
    public static final String CCCV6_DHT_CZLX = "cccv6-dht-czlx";
    // 外呼任务
    public static final String CCCV6_WHRW = "cccv6-whrw";
    // 任务信息-编辑
    public static final String CCCV6_WHRW_RWXXBJ = "cccv6-whrw-rwxxbj";
    // 任务总结-编辑
    public static final String CCCV6_WHRW_RWZJBJ = "cccv6-whrw-rwzjbj";

    //-----------------------------------电销权限----------------------------------------
    //电销
    public static final String SCC = "scc";
    //通话记录
    public static final String SCC_THJL = "scc-thjl";
    //通话详情
    public static final String SCC_THXQ = "scc-thxq";
    //通话信息
    public static final String SCC_THXQ_THXX = "scc-thxq-thxx";
    //跟进记录
    public static final String SCC_THXQ_GJJL = "scc-thxq-gjjl";
    //客户信息
    public static final String SCC_THXQ_KHXX = "scc-thxq-khxx";
    //录音播放
    public static final String SCC_THXQ_LYBF = "scc-thxq-lybf";
    //录音下载
    public static final String SCC_THXQ_LYXZ = "scc-thxq-lyxz";
    //新增客户
    public static final String SCC_THXQ_XZKH = "scc-thxq-xzkh";
    //关联客户
    public static final String SCC_THXQ_GLKH = "scc-thxq-glkh";
    //编辑客户
    public static final String SCC_THXQ_BJKH = "scc-thxq-bjkh";
    //电话回拨
    public static final String SCC_THXQ_DHHB = "scc-thxq-dhhb";
    //电销任务
    public static final String SCC_DXRW = "scc-dxrw";
    //任务信息-编辑
    public static final String SCC_DXRW_RWXXBJ = "scc-dxrw-rwxxbj";
    //任务总结-编辑
    public static final String SCC_DXRW_RWZJBJ = "scc-dxrw-rwzjbj";

    //电销 电话条
    public static final String SCC_DHT="scc-dht";
    //修改绑定电话号码
    public static final String SCC_DHT_XGBDDHHM="scc-dht-xgbddhhm";
    //修改分机账号
    public static final String SCC_DHT_XGFJZH="scc-dht-xgfjzh";
    //离线后自动解绑
    public static final String SCC_DHT_LXHZDJB="scc-dht-lxhzdjb";
    //保持和保持取消
    public static final String SCC_DHT_BCHBCQX="scc-dht-bchbcqx";
    //静音和静音取消
    public static final String SCC_DHT_JYHJYQX="scc-dht-jyhjyqx";
    //延长整理时长
    public static final String SCC_DHT_YCZLSC="scc-dht-yczlsc";
    //结束整理并置忙
    public static final String SCC_DHT_JSZLBZM="scc-dht-jszlbzm";

    public static String getCodeByType(String type) {
        if (!TextUtils.isEmpty(type)) {
            switch (type) {
                case USER_PERMISSION_TYPE_DEL_WORK_ORDER:
                    return MODULE_ORDER_DELETE;
                case USER_PERMISSION_TYPE_DEL_WORK_ORDER_REPLY:
                    return MODULE_ORDER_REPLYDELETE;
                case USER_PERMISSION_TYPE_COLSE_WORK_ORDER:
                    return MODULE_ORDER_CLOSE;
                case USER_PERMISSION_TYPE_ACTIVATION_WORK_ORDER:
                    return MODULE_ORDER_ACTIVATION;
                case USER_PERMISSION_TYPE_WORK_SEARCH:
                    return MODULE_ORDER_SEARCH;
                case USER_PERMISSION_TYPE_WORK_STATUS:
                    return MODULE_ORDER_STATUS;
                case USER_PERMISSION_TYPE_WORK_HISTORY:
                    return MODULE_ORDER_OPERATE;
                //对话权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_TALK + "":
                    return MODULE_CHAT;
                //工单权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_WORKORDER + "":
                    return MODULE_ORDER;
                //呼叫权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_CALL + "":
                    return MODULE_CALL;
                //客户中心权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_USER_CENTEER + "":
                    return MODULE_USERS;
                //监控统计权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_MONITOR_STATISTIC + "":
                    return MODULE_MONITORINGSTATISTICS;
                //监控-在线权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_MONITOR_ONLINE + "":
                    return MODULE_MONITORING_ONLINE;
                //监控-呼叫权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_MONITOR_CALL + "":
                    return MODULE_MONITORING_CALL;
                //统计-在线权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_STATISTIC_ONLINE + "":
                    return MODULE_STATISTICS_ONLINE;
                //统计-呼叫权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_STATISTIC_CALL + "":
                    return MODULE_STATISTICS_CALL;
                //统计-工单权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_STATISTIC_WORKORDER + "":
                    return MODULE_STATISTICS_ORDER;
                //统计-机器人权限
                case SobotPermissionApi.USER_PERMISSION_TYPE__STATISTIC_ROBOT + "":
                    return MODULE_STATISTICS_ROBOT;
                //设置-个人状态权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_SET_PERSON_STATUS + "":
                    return MODULE_SETTING_STATUS;
                //设置-会话顺序权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_SET_SESSION_ORDER + "":
                    return MODULE_SETTING_PLAIN;
                //工单详情页是否有右上角的编辑工单权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_EDIT_WORK_ORDER + "":
                    return MODULE_ORDER_EDIT;
                //工单详情页是否有右上角的删除工单权限
                case SobotPermissionApi.USER_PERMISSION_TYPE_DEL_WORK_ORDER + "":
                    return MODULE_ORDER_DELETE;
            }
            return type;
        } else {
            return type;
        }
    }

    public static boolean isHasPermission(String type) {
        SobotServiceInfoModel serviceInfoModel = SobotLoginTools.getInstance().getServiceInfo();
        if (serviceInfoModel != null) {
            if (serviceInfoModel.getNewBossSwitch() > 0) {
                if (!TextUtils.isEmpty(type) && serviceInfoModel != null && serviceInfoModel.getFunctionStr() != null) {
                    for (int i = 0; i < serviceInfoModel.getFunctionStr().size(); i++) {
                        ServiceFunctionVoModel cusFunctionVoModel = serviceInfoModel.getFunctionStr().get(i);
                        if (getCodeByType(type).equals(cusFunctionVoModel.getCode())) {
                            return true;
                        }
                    }
                }
            } else {
                LoginUserEntity customerServiceInfoModel = SobotLoginTools.getInstance().getLoginUser();
                if (!TextUtils.isEmpty(type) && customerServiceInfoModel != null && customerServiceInfoModel.getTicketFunction() != null && customerServiceInfoModel.getTicketFunction().containsKey(type)) {
                    return customerServiceInfoModel.getTicketFunction().get(type);
                }
            }
        }
        return false;
    }

    public static boolean isHasNewPermission(String type) {
        SobotServiceInfoModel serviceInfoModel = SobotLoginTools.getInstance().getServiceInfo();
        if (serviceInfoModel != null) {
            if (serviceInfoModel.getNewBossSwitch() > 0) {
                if (!TextUtils.isEmpty(type) && serviceInfoModel != null && serviceInfoModel.getFunctionStr() != null) {
                    for (int i = 0; i < serviceInfoModel.getFunctionStr().size(); i++) {
                        ServiceFunctionVoModel cusFunctionVoModel = serviceInfoModel.getFunctionStr().get(i);
                        if (type.equals(cusFunctionVoModel.getCode())) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
}