from ...tl.mtproto_request import MTProtoRequest


class UpdateBotPrecheckoutQuery(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    updateBotPrecheckoutQuery#5d2f3aa9 flags:# query_id:long user_id:int payload:bytes info:flags.0?PaymentRequestedInfo shipping_option_id:flags.1?string currency:string total_amount:long = Update"""

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

    def __init__(self, query_id, user_id, payload, currency, total_amount, info=None, shipping_option_id=None):
        """
        :param query_id: Telegram type: «long».
        :param user_id: Telegram type: «int».
        :param payload: Telegram type: «bytes».
        :param info: Telegram type: «PaymentRequestedInfo».
        :param shipping_option_id: Telegram type: «string».
        :param currency: Telegram type: «string».
        :param total_amount: Telegram type: «long».

        Constructor for Update: Instance of either UpdateNewMessage, UpdateMessageID, UpdateDeleteMessages, UpdateUserTyping, UpdateChatUserTyping, UpdateChatParticipants, UpdateUserStatus, UpdateUserName, UpdateUserPhoto, UpdateContactRegistered, UpdateContactLink, UpdateNewEncryptedMessage, UpdateEncryptedChatTyping, UpdateEncryption, UpdateEncryptedMessagesRead, UpdateChatParticipantAdd, UpdateChatParticipantDelete, UpdateDcOptions, UpdateUserBlocked, UpdateNotifySettings, UpdateServiceNotification, UpdatePrivacy, UpdateUserPhone, UpdateReadHistoryInbox, UpdateReadHistoryOutbox, UpdateWebPage, UpdateReadMessagesContents, UpdateChannelTooLong, UpdateChannel, UpdateNewChannelMessage, UpdateReadChannelInbox, UpdateDeleteChannelMessages, UpdateChannelMessageViews, UpdateChatAdmins, UpdateChatParticipantAdmin, UpdateNewStickerSet, UpdateStickerSetsOrder, UpdateStickerSets, UpdateSavedGifs, UpdateBotInlineQuery, UpdateBotInlineSend, UpdateEditChannelMessage, UpdateChannelPinnedMessage, UpdateBotCallbackQuery, UpdateEditMessage, UpdateInlineBotCallbackQuery, UpdateReadChannelOutbox, UpdateDraftMessage, UpdateReadFeaturedStickers, UpdateRecentStickers, UpdateConfig, UpdatePtsChanged, UpdateChannelWebPage, UpdateDialogPinned, UpdatePinnedDialogs, UpdateBotWebhookJSON, UpdateBotWebhookJSONQuery, UpdateBotShippingQuery, UpdateBotPrecheckoutQuery, UpdatePhoneCall.
        """
        super().__init__()

        self.query_id = query_id
        self.user_id = user_id
        self.payload = payload
        self.info = info
        self.shipping_option_id = shipping_option_id
        self.currency = currency
        self.total_amount = total_amount

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

        writer.write_long(self.query_id)
        writer.write_int(self.user_id)
        writer.tgwrite_bytes(self.payload)
        if self.info:
            self.info.on_send(writer)

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

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

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

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

        self.query_id = reader.read_long()
        self.user_id = reader.read_int()
        self.payload = reader.tgread_bytes()
        if (flags & (1 << 0)) != 0:
            self.info = reader.tgread_object()

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

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

    def __repr__(self):
        return 'updateBotPrecheckoutQuery#5d2f3aa9 flags:# query_id:long user_id:int payload:bytes info:flags.0?PaymentRequestedInfo shipping_option_id:flags.1?string currency:string total_amount:long = Update'

    def __str__(self):
        return '(updateBotPrecheckoutQuery (ID: 0x5d2f3aa9) = (query_id={}, user_id={}, payload={}, info={}, shipping_option_id={}, currency={}, total_amount={}))'.format(str(self.query_id), str(self.user_id), str(self.payload), str(self.info), str(self.shipping_option_id), str(self.currency), str(self.total_amount))
