from ...tl.mtproto_request import MTProtoRequest


class UpdatePinnedDialogs(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. TL definition below.
    updatePinnedDialogs#d8caf68d flags:# order:flags.0?Vector<Peer> = Update"""

    # Telegram's constructor (U)ID for this class
    constructor_id = 0xd8caf68d
    # Also the ID of its resulting type for fast checks
    subclass_of_id = 0x9f89304e

    def __init__(self, order=None):
        """
        :param order: Telegram type: "Peer". Must be a list.

        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.order = order

    def to_dict(self):
        return {
            'order': [] if self.order is None else [None if x is None else x.to_dict() for x in self.order],
        }

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

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

    @staticmethod
    def empty():
        """Returns an "empty" instance (attributes=None)"""
        return UpdatePinnedDialogs(None)

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

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

    def __repr__(self):
        return 'updatePinnedDialogs#d8caf68d flags:# order:flags.0?Vector<Peer> = Update'

    def __str__(self):
        return '(updatePinnedDialogs (ID: 0xd8caf68d) = (order={}))'.format(None if not self.order else [str(_) for _ in self.order])
