from ....tl.mtproto_request import MTProtoRequest


class ValidatedRequestedInfo(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    payments.validatedRequestedInfo#d1451883 flags:# id:flags.0?string shipping_options:flags.1?Vector<ShippingOption> = payments.ValidatedRequestedInfo"""

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

    def __init__(self, id=None, shipping_options=None):
        """
        :param id: Telegram type: «string».
        :param shipping_options: Telegram type: «ShippingOption». Must be a list.

        Constructor for payments.ValidatedRequestedInfo: Instance of ValidatedRequestedInfo.
        """
        super().__init__()

        self.id = id
        self.shipping_options = shipping_options

    def on_send(self, writer):
        writer.write_int(ValidatedRequestedInfo.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 0) if self.id else 0
        flags |= (1 << 1) if self.shipping_options else 0
        writer.write_int(flags)

        if self.id:
            writer.tgwrite_string(self.id)

        if self.shipping_options:
            writer.write_int(0x1cb5c415, signed=False)  # Vector's constructor ID
            writer.write_int(len(self.shipping_options))
            for shipping_options_item in self.shipping_options:
                if shipping_options_item:
                    shipping_options_item.on_send(writer)

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

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

        if (flags & (1 << 0)) != 0:
            self.id = reader.tgread_string()

        if (flags & (1 << 1)) != 0:
            reader.read_int()  # Vector's constructor ID
            self.shipping_options = []  # Initialize an empty list
            shipping_options_len = reader.read_int()
            for _ in range(shipping_options_len):
                shipping_options_item = reader.tgread_object()
                self.shipping_options.append(shipping_options_item)

    def __repr__(self):
        return 'payments.validatedRequestedInfo#d1451883 flags:# id:flags.0?string shipping_options:flags.1?Vector<ShippingOption> = payments.ValidatedRequestedInfo'

    def __str__(self):
        return '(payments.validatedRequestedInfo (ID: 0xd1451883) = (id={}, shipping_options={}))'.format(str(self.id), None if not self.shipping_options else [str(_) for _ in self.shipping_options])
