import typing
import copy
from .ZoneSenderObject import ZoneSenderObject

class SomeipPackage(ZoneSenderObject):
    ''' someip 发送或接收的数据
    '''
    def __init__(
        self, 
        service_name: 'str' = '',
        instance_id: 'int' = -1,
        interface_name: 'str' = '',
        interface_type: 'str' = '',
        context: 'dict' = {}) -> None:
        #########################
        super().__init__()
        self.serviceName = service_name
        self.srcIp = ''
        self.srcPort = -1
        self.destIp = ''
        self.destPort = -1
        self.interfaceType = interface_type
        self.serviceId = -1
        self.instanceId = instance_id
        self.interfaceId = -1
        self.interfaceName = interface_name
        self.context = context
    
    def __str__(self) -> str:
        # return super().__str__()
        # return '{0} {1} {2}'.format(self.serviceName, self.interfaceName, self.context)
        return \
        '''
srcIp: {0}
srcPort: {1}
destIp: {2}
destPort: {3}
type: {4}
serviceId: {5}
serviceName: {6}
instanceId: {7}
interfaceId: {8}
interfaceName: {9}
context: {10}
        '''.format(
            self.srcIp,
            self.srcPort,
            self.destIp,
            self.destPort,
            self.interfaceType,
            self.serviceId,
            self.serviceName,
            self.instanceId,
            self.interfaceId,
            self.interfaceName,
            self.context
        )

    @staticmethod
    def From(obj) -> 'SomeipPackage':
        ''' 从其他对象中构建 SomeipPackage
        :param obj: dict 表达 Someip 的 dict 对象
        '''
        if (isinstance(obj, dict)):
            someip_package_ = SomeipPackage(obj['sv_name'])
            someip_package_.srcIp = obj['src_ip']
            someip_package_.srcPort = obj['src_port']
            someip_package_.destIp = obj['dest_ip']
            someip_package_.destPort = obj['dest_port']
            someip_package_.interfaceType = obj['type']
            someip_package_.serviceId = obj['sv_id']
            someip_package_.instanceId = obj['ince_id']
            someip_package_.interfaceId = obj['if_id']
            someip_package_.interfaceName = obj['if_name']
            someip_package_.context = obj['context']
            return someip_package_
        raise TypeError('obj 的类型不是 dict')

    def ToDict(self) -> 'dict':
        return {
            'sv_name': self.serviceName,
            'src_ip': self.srcIp,
            'src_port': self.srcPort,
            'dest_ip': self.destIp,
            'dest_port': self.destPort,
            'type': self.interfaceType,
            'sv_id': self.serviceId,
            'ince_id': self.instanceId,
            'if_id': self.interfaceId,
            'if_name': self.interfaceName,
            'context': self.context,
        }