from ...tl.tlobject import TLObject


class BotInlineMessageText(TLObject):
    """Class generated by TLObjects' generator. All changes will be ERASED. 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 (U)ID for this class
    constructor_id = 0x8c7f65e2
    # Also the ID of its resulting type for fast checks
    subclass_of_id = 0xc4910f88

    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 to_dict(self):
        return {
            'no_webpage': self.no_webpage,
            'message': self.message,
            'entities': [] if self.entities is None else [None if x is None else x.to_dict() for x in self.entities],
            'reply_markup': None if self.reply_markup is None else self.reply_markup.to_dict(),
        }

    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 _x in self.entities:
                if _x:
                    _x.on_send(writer)

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

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

        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 TLObject.pretty_format(self)

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