from ....tl.mtproto_request import MTProtoRequest


class ChannelDifferenceTooLong(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    updates.channelDifferenceTooLong#410dee07 flags:# final:flags.0?true pts:int timeout:flags.1?int top_message:int read_inbox_max_id:int read_outbox_max_id:int unread_count:int messages:Vector<Message> chats:Vector<Chat> users:Vector<User> = updates.ChannelDifference"""

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

    def __init__(self, pts, top_message, read_inbox_max_id, read_outbox_max_id, unread_count, messages, chats, users, final=None, timeout=None):
        """
        :param final: Telegram type: «true».
        :param pts: Telegram type: «int».
        :param timeout: Telegram type: «int».
        :param top_message: Telegram type: «int».
        :param read_inbox_max_id: Telegram type: «int».
        :param read_outbox_max_id: Telegram type: «int».
        :param unread_count: Telegram type: «int».
        :param messages: Telegram type: «Message». Must be a list.
        :param chats: Telegram type: «Chat». Must be a list.
        :param users: Telegram type: «User». Must be a list.

        Constructor for updates.ChannelDifference: Instance of either ChannelDifferenceEmpty, ChannelDifferenceTooLong, ChannelDifference.
        """
        super().__init__()

        self.final = final
        self.pts = pts
        self.timeout = timeout
        self.top_message = top_message
        self.read_inbox_max_id = read_inbox_max_id
        self.read_outbox_max_id = read_outbox_max_id
        self.unread_count = unread_count
        self.messages = messages
        self.chats = chats
        self.users = users

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

        writer.write_int(self.pts)
        if self.timeout:
            writer.write_int(self.timeout)

        writer.write_int(self.top_message)
        writer.write_int(self.read_inbox_max_id)
        writer.write_int(self.read_outbox_max_id)
        writer.write_int(self.unread_count)
        writer.write_int(0x1cb5c415, signed=False)  # Vector's constructor ID
        writer.write_int(len(self.messages))
        for messages_item in self.messages:
            messages_item.on_send(writer)

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

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

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

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

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

        self.pts = reader.read_int()
        if (flags & (1 << 1)) != 0:
            self.timeout = reader.read_int()

        self.top_message = reader.read_int()
        self.read_inbox_max_id = reader.read_int()
        self.read_outbox_max_id = reader.read_int()
        self.unread_count = reader.read_int()
        reader.read_int()  # Vector's constructor ID
        self.messages = []  # Initialize an empty list
        messages_len = reader.read_int()
        for _ in range(messages_len):
            messages_item = reader.tgread_object()
            self.messages.append(messages_item)

        reader.read_int()  # Vector's constructor ID
        self.chats = []  # Initialize an empty list
        chats_len = reader.read_int()
        for _ in range(chats_len):
            chats_item = reader.tgread_object()
            self.chats.append(chats_item)

        reader.read_int()  # Vector's constructor ID
        self.users = []  # Initialize an empty list
        users_len = reader.read_int()
        for _ in range(users_len):
            users_item = reader.tgread_object()
            self.users.append(users_item)

    def __repr__(self):
        return 'updates.channelDifferenceTooLong#410dee07 flags:# final:flags.0?true pts:int timeout:flags.1?int top_message:int read_inbox_max_id:int read_outbox_max_id:int unread_count:int messages:Vector<Message> chats:Vector<Chat> users:Vector<User> = updates.ChannelDifference'

    def __str__(self):
        return '(updates.channelDifferenceTooLong (ID: 0x410dee07) = (final={}, pts={}, timeout={}, top_message={}, read_inbox_max_id={}, read_outbox_max_id={}, unread_count={}, messages={}, chats={}, users={}))'.format(str(self.final), str(self.pts), str(self.timeout), str(self.top_message), str(self.read_inbox_max_id), str(self.read_outbox_max_id), str(self.unread_count), None if not self.messages else [str(_) for _ in self.messages], None if not self.chats else [str(_) for _ in self.chats], None if not self.users else [str(_) for _ in self.users])
