from ...tl.mtproto_request import MTProtoRequest


class UpdateShortSentMessage(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    updateShortSentMessage#11f1331c flags:# out:flags.1?true id:int pts:int pts_count:int date:date media:flags.9?MessageMedia entities:flags.7?Vector<MessageEntity> = Updates"""

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

    def __init__(self, id, pts, pts_count, date, out=None, media=None, entities=None):
        """
        :param out: Telegram type: «true».
        :param id: Telegram type: «int».
        :param pts: Telegram type: «int».
        :param pts_count: Telegram type: «int».
        :param date: Telegram type: «date».
        :param media: Telegram type: «MessageMedia».
        :param entities: Telegram type: «MessageEntity». Must be a list.

        Constructor for Updates: Instance of either UpdatesTooLong, UpdateShortMessage, UpdateShortChatMessage, UpdateShort, UpdatesCombined, UpdatesTg, UpdateShortSentMessage.
        """
        super().__init__()

        self.out = out
        self.id = id
        self.pts = pts
        self.pts_count = pts_count
        self.date = date
        self.media = media
        self.entities = entities

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

        writer.write_int(self.id)
        writer.write_int(self.pts)
        writer.write_int(self.pts_count)
        writer.tgwrite_date(self.date)
        if self.media:
            self.media.on_send(writer)

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

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

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

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

        self.id = reader.read_int()
        self.pts = reader.read_int()
        self.pts_count = reader.read_int()
        self.date = reader.tgread_date()
        if (flags & (1 << 9)) != 0:
            self.media = reader.tgread_object()

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

    def __repr__(self):
        return 'updateShortSentMessage#11f1331c flags:# out:flags.1?true id:int pts:int pts_count:int date:date media:flags.9?MessageMedia entities:flags.7?Vector<MessageEntity> = Updates'

    def __str__(self):
        return '(updateShortSentMessage (ID: 0x11f1331c) = (out={}, id={}, pts={}, pts_count={}, date={}, media={}, entities={}))'.format(str(self.out), str(self.id), str(self.pts), str(self.pts_count), str(self.date), str(self.media), None if not self.entities else [str(_) for _ in self.entities])
