from ...tl.mtproto_request import MTProtoRequest


class MessageMediaInvoice(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    messageMediaInvoice#84551347 flags:# shipping_address_requested:flags.1?true test:flags.3?true title:string description:string photo:flags.0?WebDocument receipt_msg_id:flags.2?int currency:string total_amount:long start_param:string = MessageMedia"""

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

    def __init__(self, title, description, currency, total_amount, start_param, shipping_address_requested=None, test=None, photo=None, receipt_msg_id=None):
        """
        :param shipping_address_requested: Telegram type: «true».
        :param test: Telegram type: «true».
        :param title: Telegram type: «string».
        :param description: Telegram type: «string».
        :param photo: Telegram type: «WebDocument».
        :param receipt_msg_id: Telegram type: «int».
        :param currency: Telegram type: «string».
        :param total_amount: Telegram type: «long».
        :param start_param: Telegram type: «string».

        Constructor for MessageMedia: Instance of either MessageMediaEmpty, MessageMediaPhoto, MessageMediaGeo, MessageMediaContact, MessageMediaUnsupported, MessageMediaDocument, MessageMediaWebPage, MessageMediaVenue, MessageMediaGame, MessageMediaInvoice.
        """
        super().__init__()

        self.shipping_address_requested = shipping_address_requested
        self.test = test
        self.title = title
        self.description = description
        self.photo = photo
        self.receipt_msg_id = receipt_msg_id
        self.currency = currency
        self.total_amount = total_amount
        self.start_param = start_param

    def on_send(self, writer):
        writer.write_int(MessageMediaInvoice.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 1) if self.shipping_address_requested else 0
        flags |= (1 << 3) if self.test else 0
        flags |= (1 << 0) if self.photo else 0
        flags |= (1 << 2) if self.receipt_msg_id else 0
        writer.write_int(flags)

        writer.tgwrite_string(self.title)
        writer.tgwrite_string(self.description)
        if self.photo:
            self.photo.on_send(writer)

        if self.receipt_msg_id:
            writer.write_int(self.receipt_msg_id)

        writer.tgwrite_string(self.currency)
        writer.write_long(self.total_amount)
        writer.tgwrite_string(self.start_param)

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

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

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

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

        self.title = reader.tgread_string()
        self.description = reader.tgread_string()
        if (flags & (1 << 0)) != 0:
            self.photo = reader.tgread_object()

        if (flags & (1 << 2)) != 0:
            self.receipt_msg_id = reader.read_int()

        self.currency = reader.tgread_string()
        self.total_amount = reader.read_long()
        self.start_param = reader.tgread_string()

    def __repr__(self):
        return 'messageMediaInvoice#84551347 flags:# shipping_address_requested:flags.1?true test:flags.3?true title:string description:string photo:flags.0?WebDocument receipt_msg_id:flags.2?int currency:string total_amount:long start_param:string = MessageMedia'

    def __str__(self):
        return '(messageMediaInvoice (ID: 0x84551347) = (shipping_address_requested={}, test={}, title={}, description={}, photo={}, receipt_msg_id={}, currency={}, total_amount={}, start_param={}))'.format(str(self.shipping_address_requested), str(self.test), str(self.title), str(self.description), str(self.photo), str(self.receipt_msg_id), str(self.currency), str(self.total_amount), str(self.start_param))
