#!/usr/bin/env python
# -*- coding:utf-8 -*-


CODEMSG = {
    2000: u"True",
    4000: u"客户上传的文件格式不正确",
    4001: u"客户上传的文件列超过1024",
    4002: u"客户端未传值!",
    4003: u"文件数据格式不对",
    4004: u"已经存在",
    4005: u"用户未认证或认证不成功",
    4006: u"无法进行数据分析",
    4007: u"用户分析条件不正确",
    4008: u"用户无权限",
    4009: u"值传递错误!!!",
    4010: u"调用权限接口返回不允许状态",
    4011: u"长度不一致（LENGTH ERROR！！！）",
    4012: u"无数据导致错误（NOT DATA ERROR！！！）",
    4013: u"无文件导致错误（NOT FILE ERROR！！！）",
    4014: u"秘钥解密失败",
    4015: u"因变量应为二分数值",
    4022: u"分组变量应为二分数值",
    4016: u"请等待管理员发送",
    4017: u"下载申请已提交请等待审核",
    4018: u"线上审核后线上下载",
    4019: u"线上审核后邮件发送",
    4020: u"仅供分析探索不提供原始数据",
    4021: u"权限错误",
    4023: u"有重复变量，请修改后再次上传！！！",
    4024: u"请更改变量名， 此变量名‘mycustomid’为系统关键字",
    4025: u"收藏夹内无变量",
    4026: u"无法分割字符",
    4027: u"",
    4999: u"Unknown Error!!!",
    5000: u"服务器错误",
    5001: u"数据表已经存在",
    5002: u"SQL语句错误",
    5003: u"索引文件未创建",
    6001: u"模板文件不存在",
    6002: u"未设置模板数据"
}


class MyCustomError(Exception):
    """
    Error class for the IBM SPSS Statistics Input Output Module
    Use Method:

    def example():
        try:
            raise ValueError # ['']
            raise SPSSError(retcode="6001") or  raise SPSSError(retcode=6001)
        except Exception as e:
            e = FormatErrorCode(e)
            print(e) # ['4999', 'Unknown Error!!!']

    example()

    #>>> (5001, '数据表已经存在')

    """

    def __init__(self, msg=None, retcode=4999):
        self.retcode = int(retcode)
        try:
            if not msg:
                msg = CODEMSG[int(self.retcode)]
        except:
            msg = "Unknown Error!!!"
        Exception.__init__(self, self.retcode, msg)


def FormatErrorCode(arg):
    # print(arg)
    data = str(arg).replace("(", "").replace(")", "").replace("'", '').replace('"', '').split(",")
    # print(data)
    if all(data):
        Error_Infor = [i.strip() for i in data if i]
        if len(Error_Infor) == 2:
            if data[0] == '1264' or "list index out of range" in data[1]:
                return ['4027', data[1]]
            else:
                return [i.strip() for i in data if i]
        else:
            try:
                if str(arg) == "list index out of range":
                    raise MyCustomError(retcode=4009)
                else:
                    raise MyCustomError(msg=Error_Infor[0])
            except Exception as e:
                return FormatErrorCode(e)
    else:
        try:
            raise MyCustomError()
        except Exception as e:
            return FormatErrorCode(e)


class SPSSError(MyCustomError):
    def __init__(self, retcode=None):
        super(SPSSError, self).__init__(retcode=retcode)


class GetParametersError(MyCustomError):
    def __init__(self, retcode=4002):
        super(GetParametersError, self).__init__(retcode=retcode)


class LoginInError(MyCustomError):
    def __init__(self, retcode=4005):
        super(LoginInError, self).__init__(retcode=retcode)

class PermissionNotError(MyCustomError):
    def __init__(self, retcode=4008):
        super(PermissionNotError, self).__init__(retcode=retcode)

# ----<值传递错误， 针对前端传值使用>-----#
class ValuePropagetionError(MyCustomError):
    def __init__(self, retcode=4009):
        super(ValuePropagetionError, self).__init__(retcode=retcode)

