from telethon.tl.mtproto_request import MTProtoRequest


class ChannelMessagesFilter(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    channelMessagesFilter#cd77d957 flags:None exclude_new_messages:flags.1?true ranges:Vector<MessageRange> = ChannelMessagesFilter"""

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

    def __init__(self, ranges, exclude_new_messages=None):
        """
        :param exclude_new_messages: Telegram type: «true».
        :param ranges: Telegram type: «MessageRange». Must be a list.
        """
        super().__init__()

        self.exclude_new_messages = exclude_new_messages
        self.ranges = ranges

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

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

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

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

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

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

    def __repr__(self):
        return 'channelMessagesFilter#cd77d957 flags:None exclude_new_messages:flags.1?true ranges:Vector<MessageRange> = ChannelMessagesFilter'

    def __str__(self):
        return '(channelMessagesFilter (ID: 0xcd77d957) = (exclude_new_messages={}, ranges={}))'.format(str(self.exclude_new_messages), None if not self.ranges else [str(_) for _ in self.ranges])
