# -*- coding:utf-8 -*-
"""
@Author  : xuxingchen
@Contact : xuxingchen@sinochem.com
@Desc    : None
"""
import json
import time


class BaseResp:
    def __init__(self, message_id, data):
        self.message_id = message_id
        self.time = int(time.time() * 1000)
        self.data = data

    def dict(self):
        if not isinstance(self.data, list):
            if isinstance(self.data, dict):
                data = self.data
            else:
                data = self.data()
        else:
            data = [i if isinstance(i, dict) else i() for i in self.data]
        return {
            "messageId": self.message_id,
            "time": self.time,
            "data": data
        }

    def __call__(self):
        return json.dumps(self.dict())


class SimpleResp:
    def __init__(self, message_id, code):
        self.message_id = message_id
        self.time = int(time.time() * 1000)
        self.code = code

    def dict(self):
        return {
            "messageId": self.message_id,
            "time": self.time,
            "code": self.code
        }

    def __call__(self, *args, **kwargs):
        return json.dumps(self.dict())


class ErrorResp:
    def __init__(self, message_id, code):
        self.message_id = message_id
        self.time = int(time.time() * 1000)
        self.code = code

    def dict(self):
        return {
            "messageId": self.message_id,
            "time": self.time,
            "data": {"code": self.code}
        }

    def __call__(self, *args, **kwargs):
        return json.dumps(self.dict())


class DRRespItem:
    """设备注册返回数据体"""

    def __init__(self, device_id: str, device_name: str, device_secret: str, code: int):
        self.device_id = device_id
        self.device_name = device_name
        self.device_secret = device_secret
        self.code = code

    def __call__(self, *args, **kwargs):
        return {
            "deviceId": self.device_id,
            "deviceName": self.device_name,
            "deviceSecret": self.device_secret,
            "code": self.code
        }


class SDRRespItem:
    """设备注册返回数据体"""

    def __init__(self, device_id: str, device_name: str, code: int):
        self.device_id = device_id
        self.device_name = device_name
        self.code = code

    def __call__(self, *args, **kwargs):
        return {
            "deviceId": self.device_id,
            "deviceName": self.device_name,
            "code": self.code
        }
