from ...tl.tlobject import TLObject


class Message(TLObject):
    """Class generated by TLObjects' generator. All changes will be ERASED. TL definition below.
    message#90dddc11 flags:# out:flags.1?true mentioned:flags.4?true media_unread:flags.5?true silent:flags.13?true post:flags.14?true id:int from_id:flags.8?int to_id:Peer fwd_from:flags.2?MessageFwdHeader via_bot_id:flags.11?int reply_to_msg_id:flags.3?int date:int message:string media:flags.9?MessageMedia reply_markup:flags.6?ReplyMarkup entities:flags.7?Vector<MessageEntity> views:flags.10?int edit_date:flags.15?int post_author:flags.16?string = Message"""

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

    def __init__(self, id, to_id, date, message, out=None, mentioned=None, media_unread=None, silent=None, post=None, from_id=None, fwd_from=None, via_bot_id=None, reply_to_msg_id=None, media=None, reply_markup=None, entities=None, views=None, edit_date=None, post_author=None):
        """
        :param out: Telegram type: "true".
        :param mentioned: Telegram type: "true".
        :param media_unread: Telegram type: "true".
        :param silent: Telegram type: "true".
        :param post: Telegram type: "true".
        :param id: Telegram type: "int".
        :param from_id: Telegram type: "int".
        :param to_id: Telegram type: "Peer".
        :param fwd_from: Telegram type: "MessageFwdHeader".
        :param via_bot_id: Telegram type: "int".
        :param reply_to_msg_id: Telegram type: "int".
        :param date: Telegram type: "date".
        :param message: Telegram type: "string".
        :param media: Telegram type: "MessageMedia".
        :param reply_markup: Telegram type: "ReplyMarkup".
        :param entities: Telegram type: "MessageEntity". Must be a list.
        :param views: Telegram type: "int".
        :param edit_date: Telegram type: "date".
        :param post_author: Telegram type: "string".

        Constructor for Message: Instance of either MessageEmpty, Message, MessageService.
        """
        super().__init__()

        self.out = out
        self.mentioned = mentioned
        self.media_unread = media_unread
        self.silent = silent
        self.post = post
        self.id = id
        self.from_id = from_id
        self.to_id = to_id
        self.fwd_from = fwd_from
        self.via_bot_id = via_bot_id
        self.reply_to_msg_id = reply_to_msg_id
        self.date = date
        self.message = message
        self.media = media
        self.reply_markup = reply_markup
        self.entities = entities
        self.views = views
        self.edit_date = edit_date
        self.post_author = post_author

    def to_dict(self):
        return {
            'out': self.out,
            'mentioned': self.mentioned,
            'media_unread': self.media_unread,
            'silent': self.silent,
            'post': self.post,
            'id': self.id,
            'from_id': self.from_id,
            'to_id': None if self.to_id is None else self.to_id.to_dict(),
            'fwd_from': None if self.fwd_from is None else self.fwd_from.to_dict(),
            'via_bot_id': self.via_bot_id,
            'reply_to_msg_id': self.reply_to_msg_id,
            'date': self.date,
            'message': self.message,
            'media': None if self.media is None else self.media.to_dict(),
            'reply_markup': None if self.reply_markup is None else self.reply_markup.to_dict(),
            'entities': [] if self.entities is None else [None if x is None else x.to_dict() for x in self.entities],
            'views': self.views,
            'edit_date': self.edit_date,
            'post_author': self.post_author,
        }

    def on_send(self, writer):
        writer.write_int(Message.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 << 4) if self.mentioned else 0
        flags |= (1 << 5) if self.media_unread else 0
        flags |= (1 << 13) if self.silent else 0
        flags |= (1 << 14) if self.post else 0
        flags |= (1 << 8) if self.from_id else 0
        flags |= (1 << 2) if self.fwd_from else 0
        flags |= (1 << 11) if self.via_bot_id else 0
        flags |= (1 << 3) if self.reply_to_msg_id else 0
        flags |= (1 << 9) if self.media else 0
        flags |= (1 << 6) if self.reply_markup else 0
        flags |= (1 << 7) if self.entities else 0
        flags |= (1 << 10) if self.views else 0
        flags |= (1 << 15) if self.edit_date else 0
        flags |= (1 << 16) if self.post_author else 0
        writer.write_int(flags)

        writer.write_int(self.id)
        if self.from_id:
            writer.write_int(self.from_id)

        self.to_id.on_send(writer)
        if self.fwd_from:
            self.fwd_from.on_send(writer)

        if self.via_bot_id:
            writer.write_int(self.via_bot_id)

        if self.reply_to_msg_id:
            writer.write_int(self.reply_to_msg_id)

        writer.tgwrite_date(self.date)
        writer.tgwrite_string(self.message)
        if self.media:
            self.media.on_send(writer)

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

        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.views:
            writer.write_int(self.views)

        if self.edit_date:
            writer.tgwrite_date(self.edit_date)

        if self.post_author:
            writer.tgwrite_string(self.post_author)

    @staticmethod
    def empty():
        """Returns an "empty" instance (attributes=None)"""
        return Message(None, None, None, None, None, None, None, None, None, None, None, None, 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

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

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

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

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

        self.id = reader.read_int()
        if (flags & (1 << 8)) != 0:
            self.from_id = reader.read_int()

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

        if (flags & (1 << 11)) != 0:
            self.via_bot_id = reader.read_int()

        if (flags & (1 << 3)) != 0:
            self.reply_to_msg_id = reader.read_int()

        self.date = reader.tgread_date()
        self.message = reader.tgread_string()
        if (flags & (1 << 9)) != 0:
            self.media = reader.tgread_object()

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

        if (flags & (1 << 7)) != 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 << 10)) != 0:
            self.views = reader.read_int()

        if (flags & (1 << 15)) != 0:
            self.edit_date = reader.tgread_date()

        if (flags & (1 << 16)) != 0:
            self.post_author = reader.tgread_string()

    def __repr__(self):
        return 'message#90dddc11 flags:# out:flags.1?true mentioned:flags.4?true media_unread:flags.5?true silent:flags.13?true post:flags.14?true id:int from_id:flags.8?int to_id:Peer fwd_from:flags.2?MessageFwdHeader via_bot_id:flags.11?int reply_to_msg_id:flags.3?int date:int message:string media:flags.9?MessageMedia reply_markup:flags.6?ReplyMarkup entities:flags.7?Vector<MessageEntity> views:flags.10?int edit_date:flags.15?int post_author:flags.16?string = Message'

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

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