from ...tl.tlobject import TLObject


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

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

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

        Constructor for ChannelMessagesFilter: Instance of either ChannelMessagesFilterEmpty, ChannelMessagesFilter.
        """
        super().__init__()

        self.exclude_new_messages = exclude_new_messages
        self.ranges = ranges

    def to_dict(self):
        return {
            'exclude_new_messages': self.exclude_new_messages,
            'ranges': [] if self.ranges is None else [None if x is None else x.to_dict() for x in self.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 _x in self.ranges:
            _x.on_send(writer)

    @staticmethod
    def empty():
        """Returns an "empty" instance (attributes=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
        _len = reader.read_int()
        for _ in range(_len):
            _x = reader.tgread_object()
            self.ranges.append(_x)

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

    def __str__(self):
        return TLObject.pretty_format(self)

    def stringify(self):
        return TLObject.pretty_format(self, indent=0)
