from ...tl.mtproto_request import MTProtoRequest


class DcOption(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    dcOption#05d8c6cc flags:# ipv6:flags.0?true media_only:flags.1?true tcpo_only:flags.2?true cdn:flags.3?true id:int ip_address:string port:int = DcOption"""

    # Telegram's constructor ID (and unique identifier) for this class
    constructor_id = 0x5d8c6cc

    def __init__(self, id, ip_address, port, ipv6=None, media_only=None, tcpo_only=None, cdn=None):
        """
        :param ipv6: Telegram type: «true».
        :param media_only: Telegram type: «true».
        :param tcpo_only: Telegram type: «true».
        :param cdn: Telegram type: «true».
        :param id: Telegram type: «int».
        :param ip_address: Telegram type: «string».
        :param port: Telegram type: «int».

        Constructor for DcOption: Instance of DcOption.
        """
        super().__init__()

        self.ipv6 = ipv6
        self.media_only = media_only
        self.tcpo_only = tcpo_only
        self.cdn = cdn
        self.id = id
        self.ip_address = ip_address
        self.port = port

    def on_send(self, writer):
        writer.write_int(DcOption.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 0) if self.ipv6 else 0
        flags |= (1 << 1) if self.media_only else 0
        flags |= (1 << 2) if self.tcpo_only else 0
        flags |= (1 << 3) if self.cdn else 0
        writer.write_int(flags)

        writer.write_int(self.id)
        writer.tgwrite_string(self.ip_address)
        writer.write_int(self.port)

    @staticmethod
    def empty():
        """Returns an "empty" instance (all attributes are None)"""
        return DcOption(None, None, None, None, None, None, None)

    def on_response(self, reader):
        flags = reader.read_int()

        if (flags & (1 << 0)) != 0:
            self.ipv6 = True  # Arbitrary not-None value, no need to read since it is a flag

        if (flags & (1 << 1)) != 0:
            self.media_only = True  # Arbitrary not-None value, no need to read since it is a flag

        if (flags & (1 << 2)) != 0:
            self.tcpo_only = True  # Arbitrary not-None value, no need to read since it is a flag

        if (flags & (1 << 3)) != 0:
            self.cdn = True  # Arbitrary not-None value, no need to read since it is a flag

        self.id = reader.read_int()
        self.ip_address = reader.tgread_string()
        self.port = reader.read_int()

    def __repr__(self):
        return 'dcOption#05d8c6cc flags:# ipv6:flags.0?true media_only:flags.1?true tcpo_only:flags.2?true cdn:flags.3?true id:int ip_address:string port:int = DcOption'

    def __str__(self):
        return '(dcOption (ID: 0x5d8c6cc) = (ipv6={}, media_only={}, tcpo_only={}, cdn={}, id={}, ip_address={}, port={}))'.format(str(self.ipv6), str(self.media_only), str(self.tcpo_only), str(self.cdn), str(self.id), str(self.ip_address), str(self.port))
