from ...tl.mtproto_request import MTProtoRequest


class MessageService(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    messageService#9e19a1f6 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 reply_to_msg_id:flags.3?int date:date action:MessageAction = Message"""

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

    def __init__(self, id, to_id, date, action, out=None, mentioned=None, media_unread=None, silent=None, post=None, from_id=None, reply_to_msg_id=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 reply_to_msg_id: Telegram type: «int».
        :param date: Telegram type: «date».
        :param action: Telegram type: «MessageAction».

        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.reply_to_msg_id = reply_to_msg_id
        self.date = date
        self.action = action

    def on_send(self, writer):
        writer.write_int(MessageService.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 << 3) if self.reply_to_msg_id 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.reply_to_msg_id:
            writer.write_int(self.reply_to_msg_id)

        writer.tgwrite_date(self.date)
        self.action.on_send(writer)

    @staticmethod
    def empty():
        """Returns an "empty" instance (all attributes are None)"""
        return MessageService(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 << 3)) != 0:
            self.reply_to_msg_id = reader.read_int()

        self.date = reader.tgread_date()
        self.action = reader.tgread_object()

    def __repr__(self):
        return 'messageService#9e19a1f6 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 reply_to_msg_id:flags.3?int date:date action:MessageAction = Message'

    def __str__(self):
        return '(messageService (ID: 0x9e19a1f6) = (out={}, mentioned={}, media_unread={}, silent={}, post={}, id={}, from_id={}, to_id={}, reply_to_msg_id={}, date={}, action={}))'.format(str(self.out), str(self.mentioned), str(self.media_unread), str(self.silent), str(self.post), str(self.id), str(self.from_id), str(self.to_id), str(self.reply_to_msg_id), str(self.date), str(self.action))
