import simplejson as json
from django.shortcuts import HttpResponse


class ResponseObject(object):
    def __init__(self, lang='en', returntype='currency'):
        self.lang = lang
        self.returntype = returntype

    def data(self, code, res=None):
        if res is None:
            res = {}

        data_en = {
            0: 'Success',
            5: 'Please try again one minute later!',
            10: res,
            12: 'You are not the primary user of the device!',
            13: 'Timestamp token verification failed',
            14: 'Device is not belong to you',
            15: 'Device has been bound',
            16: 'WeChat has been bound, please log in and unbind using WeChat',
            17: 'No email or mobile phone login',
            18: 'User not logged in',
            44: 'System error! Can not send email',
            48: 'System object error!',
            89: 'Already send the code, please check it or get it again after 10m',
            90: 'please check code or get it again after 1m',
            99: 'Mail doesn\'t exist!',
            100: 'Phone format error!',
            101: 'Phone already existed!',
            102: 'Phone doesn\'t exist!',
            103: 'Mail already existed!',
            104: 'Account doesn\'t exist!',
            105: 'Email format error!',
            106: 'Illegal profile picture!',
            107: 'The username not conform to the rules!',
            108: 'Illegal nickname!',
            109: 'The password not conform to the rules!',
            110: 'user doesn\'t activated',
            111: 'Error password',
            112: 'Fingerprint login is not turned on',
            119: 'The qr code has expired',
            120: 'The code has expired',
            121: 'The verification code is wrong!',
            173: 'Data does not exists!',
            174: 'Data already exists!',
            176: 'Delete error',
            177: 'Update error',
            178: 'ADD error',
            179: 'Nickname repeated',
            180: 'Smart button scene trigger conditions cannot be repeated',
            181: 'The gateway only bind 3 smart button at most',
            201: 'You can only add 3 custom voice at most',
            306: 'The link has expired!',
            309: 'Please ReLogin! errmsg token',
            404: 'You don not have permission to access this!',
            414: 'Please confirm the request url!',
            424: 'Database Error !',
            444: 'Wrong parameters!',
            474: 'System Maintaining!',
            475: 'App Version too low, please upgrade!',
            500: 'Internal error!',
            700: 'Upload file error',
            701: 'The file does not exist!',
            711: 'Do not downgrade',
            712: 'Area needs to be consistent',
            713: 'Storage rules cannot be changed during the validity period',
            717: 'Authorization expires',
            800: 'Order does not exist',
            801: 'The refund amount cannot be greater than the order price',
            802: 'The order has been completed and cannot be cancelled',
            804: 'Refund, please do not repeat the operation',
            805: 'Could not refund',
            900: 'There is no information about this version!',
            901: 'Getting URL failure!',
            902: 'No update!',
            903: 'Error filename',
            904: 'Version does not support this feature!',
            906: 'Cause of file operation error',
            907: 'The download file does not exist!',
            910: 'Icloud Insufficient capacity!',
            911: 'No opened Icloud',
            10001: 'Customer number, customer confidentiality error',
            10002: 'Check your configuration: no customer number, customer confidentiality',
            10003: 'The authorization code does not exist. Please reauthorize',
            10004: 'The request method is incorrect. Please contact the developer',
            10005: 'Wrong configuration, wrong customer number',
            10006: 'Configuration error. The path value is incorrect',
            10007: 'This device is not an experience package and cannot be reset',
            10008: 'The user does not have this device and cannot transfer',
            10009: 'The user already owns the device and cannot transfer it',
            10010: 'Devices that are not under the same account cannot be transferred',
            10011: 'Receiving transfer device does not exist and cannot be transferred',
            10012: 'Experience packages cannot be transferred',
            10013: 'Original equipment package has expired and cannot be transferred',
            10014: 'Accept transfer of equipment packages that have not expired and cannot be transferred',
            10015: 'Shared devices cannot be transferred',
            10030: 'No purchase of cloud storage',
            10031: 'The cloud storage has expired',
            10032: 'The switched cloud storage package ID cannot be the same as the one in use',
            10033: 'The primary user of the device cannot purchase it',
            10034: 'Non device primary users cannot view cloud storage',
            10035: 'Non device primary users cannot experience cloud storage',
            10036: 'Non device primary users cannot exchange for cloud storage',
            10037: 'Non device primary user cannot transfer device',
            10038: 'Non device primary user cannot transfer packages',
            10039: 'Activation code has been used',
            10040: 'Invalid activation code',
            10041: 'This device has purchased a domestic cloud storage package, and cannot purchase a foreign cloud storage package',
            10042: 'The device has registered a certificate',
            10043: 'The device does not registered a certificate',
            10044: 'Failed to publish MQTT message',
            10045: 'Already the latest version',
            10046: 'Sorry, users who have activated cloud storage packages do not support logout at the moment, please contact customer service',
            10047: 'Please delete all devices under your account first',
            10048: 'Subscribe to the failure',
            10049: 'The coupon is not available',
            10050: 'You have subscribed to this package, please do not subscribe again',
            10051: 'Unsubscribe successfully',
            10052: 'Unsubscribe failed',
            10053: 'The AI service is not purchased',
            10054: 'The AI service has expired',
            10055: 'The AI does not recognize any labels',
            10056: 'The device has enabled automatic renewal of cloud storage package and cannot be transferred for the time being',
            10057: "Can't delete",
            10058: 'Default family cannot be deleted',
            10059: 'Order deactivation failure',
            10060: 'This device has purchased foreign cloud storage package, so it cannot buy domestic cloud storage package',
            10061: 'Add the limit reached',
            10062: 'This device has experienced the package',
            10063: 'Failed to claim',
            10064: 'If the claim fails, please contact customer service',
            10065: 'The redemption is successful and can be viewed in Settings-4G-My Package',
            10066: 'This code has been redeemed and can be viewed in Settings-4G-My Package',
            10067: 'Invalid redemption code',
            10068: 'This device does not support redemption, please contact customer service'
        }
        data_cn = {
            0: '成功',
            5: '请一分钟后再尝试',
            10: res,
            12: '非设备主用户',
            13: '时间戳token校验失败',
            14: '设备不属于您',
            15: '设备已被绑定',
            16: '微信已被绑定，请使用微信登录并解绑',
            17: '未绑定邮箱或者手机登录方式',
            18: '用户未登录',
            44: '系统错误！无法发送电子邮件',
            48: '系统对象错误',
            89: '已发验证码，请检测或10分钟后重新获取。',
            90: '请检测或1分钟后重新获取。',
            99: '邮箱不存在！',
            100: '手机格式错误！',
            101: '手机已存在！',
            102: '手机不存在！',
            103: '邮箱已存在！',
            104: '账户不存在！',
            105: '邮箱格式错误！',
            106: '头像违规！',
            107: '用户名格式不符合！',
            108: '用户昵称违规！',
            109: '密码格式不符合！',
            110: '用户未激活！',
            111: '密码不正确！',
            112: '未开通指纹登录',
            119: '二维码过期',
            120: '验证码过期',
            121: '验证码错了！',
            173: '数据不存在！',
            174: '数据已存在！',
            176: '删除错误',
            177: '更新错误',
            178: '增加错误',
            179: '名称不能重复',
            180: '智能按钮场景触发条件不能重复',
            181: '该网关最多只能绑定3个智能按钮',
            201: '最多只能添加3条自定义语音',
            306: '链接已超过有效期!',
            309: '请重新登录!',
            404: '您没有访问的权限！',
            414: '请确认请求url！',
            424: '数据库错误！',
            444: '参数错误！',
            474: '系统维护中！',
            475: '版本过低，请升级程序！',
            500: '内部错误！',
            700: '上传文件错误',
            701: '文件不存在',
            711: '不可降级',
            712: '区域不一致',
            713: '有效期内不可更改存储规则',
            717: '授权过期',
            800: '订单不存在',
            801: '退款金额不能大于订单价格',
            802: '订单已完成，不能撤销',
            804: '订单已退款，请勿重复操作',
            805: '无法退款',
            900: '版本信息不存在',
            901: '获取链接失败',
            902: '无更新！',
            903: '文件名不符合！',
            904: '版本不支持本功能!',
            906: '文件操作错误',
            907: '文件不存在！',
            910: '云盘容量不足！',
            911: '未开通云盘！',
            10001: '客户编号,客户机密错误',
            10002: '检查您的配置:没有客户编号,客户机密',
            10003: '授权码不存在，请重新授权',
            10004: '请求方法不正确。请联系开发者',
            10005: '配置错误，客户编号这个值是错误的',
            10006: '配置错误，路径这个值是错误的',
            10007: '此设备不是体验套餐，无法重置',
            10008: '用户没有此设备，无法转移',
            10009: '用户已拥有此设备，无法转移',
            10010: '不是同一账户下的设备，无法转移',
            10011: '接受转移设备不存在，无法转移',
            10012: '体验套餐无法转移',
            10013: '原设备套餐已过期，无法转移',
            10014: '接受转移设备套餐未过期，无法转移',
            10015: '分享的设备无法转移',
            10030: '未购买云存',
            10031: '云存已过期',
            10032: '切换的云存套餐ID不能与正在使用中的相同',
            10033: '非设备主用户无法购买',
            10034: '非设备主用户无法查看云存',
            10035: '非设备主用户无法体验云存',
            10036: '非设备主用户无法兑换云存',
            10037: '非设备主用户无法转移设备',
            10038: '非设备主用户无法转移套餐',
            10039: '激活码已被使用过',
            10040: '无效激活码',
            10041: '此设备已购买过国内云存套餐，无法购买国外云存套餐',
            10042: '此设备已注册证书',
            10043: '此设备没有注册证书',
            10044: '发布MQTT消息失败',
            10045: '当前为最新版本',
            10046: '已开通云存的用户，暂不支持注销，请联系客服',
            10047: '请先删除您当前帐户下的所有设备',
            10048: '订阅失败',
            10049: '该优惠券不可用',
            10050: '您已订阅过此套餐，请不要重复订阅',
            10051: '已取消订阅',
            10052: '订阅取消失败',
            10053: '未购买AI服务',
            10054: 'AI服务已过期',
            10055: 'AI没有识别到任何标签',
            10056: '该设备已开通云存套餐自动续费，暂时无法转移',
            10057: '不能删除',
            10058: '默认家庭不能删除',
            10059: '订单停用失败',
            10060: '此设备已购买过国外云存套餐，无法购买国内云存套餐',
            10061: '添加已达到上限',
            10062: '此设备已体验过套餐',
            10063: '领取失败',
            10064: '领取失败,请联系客服',
            10065: '\t兑换成功\n可在设置-4G-我的套餐中查看',
            10066: '\t此码已兑换\n可在设置-4G-我的套餐中查看',
            10067: '无效兑换码',
            10068: '兑换失败,请联系客服'
        }

        msg = data_cn if self.lang == 'cn' or self.lang == 'zh-Hans' or self.lang == 'zh-Hant' else data_en

        reason = msg.get(code)
        if reason is None:
            reason = 'code不存在'

        if self.returntype == 'pc':
            return {'code': code, 'msg': reason, 'data': res, 'error_code': code}
        return {'result_code': code, 'reason': reason, 'result': res, 'error_code': code}

    def formal(self, code, res=None):
        if res is None:
            res = {}
        formal_data = self.data(code, res)
        return json.dumps(formal_data, ensure_ascii=False)

    def json(self, code, res=None):
        if res is None:
            res = {}
        result = self.formal(code, res)
        return HttpResponse(result)
