from ...tl.mtproto_request import MTProtoRequest


class BotInlineMessageText(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    botInlineMessageText#8c7f65e2 flags:# no_webpage:flags.0?true message:string entities:flags.1?Vector<MessageEntity> reply_markup:flags.2?ReplyMarkup = BotInlineMessage"""

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

    def __init__(self, message, no_webpage=None, entities=None, reply_markup=None):
        """
        :param no_webpage: Telegram type: «true».
        :param message: Telegram type: «string».
        :param entities: Telegram type: «MessageEntity». Must be a list.
        :param reply_markup: Telegram type: «ReplyMarkup».

        Constructor for BotInlineMessage: Instance of either BotInlineMessageMediaAuto, BotInlineMessageText, BotInlineMessageMediaGeo, BotInlineMessageMediaVenue, BotInlineMessageMediaContact.
        """
        super().__init__()

        self.no_webpage = no_webpage
        self.message = message
        self.entities = entities
        self.reply_markup = reply_markup

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

        writer.tgwrite_string(self.message)
        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)

        if self.reply_markup:
            self.reply_markup.on_send(writer)

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

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

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

        self.message = reader.tgread_string()
        if (flags & (1 << 1)) != 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)

        if (flags & (1 << 2)) != 0:
            self.reply_markup = reader.tgread_object()

    def __repr__(self):
        return 'botInlineMessageText#8c7f65e2 flags:# no_webpage:flags.0?true message:string entities:flags.1?Vector<MessageEntity> reply_markup:flags.2?ReplyMarkup = BotInlineMessage'

    def __str__(self):
        return '(botInlineMessageText (ID: 0x8c7f65e2) = (no_webpage={}, message={}, entities={}, reply_markup={}))'.format(str(self.no_webpage), str(self.message), None if not self.entities else [str(_) for _ in self.entities], str(self.reply_markup))
