from ...tl.mtproto_request import MTProtoRequest


class Dialog(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    dialog#66ffba14 flags:# pinned:flags.2?true peer:Peer top_message:int read_inbox_max_id:int read_outbox_max_id:int unread_count:int notify_settings:PeerNotifySettings pts:flags.0?int draft:flags.1?DraftMessage = Dialog"""

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

    def __init__(self, peer, top_message, read_inbox_max_id, read_outbox_max_id, unread_count, notify_settings, pinned=None, pts=None, draft=None):
        """
        :param pinned: Telegram type: «true».
        :param peer: Telegram type: «Peer».
        :param top_message: Telegram type: «int».
        :param read_inbox_max_id: Telegram type: «int».
        :param read_outbox_max_id: Telegram type: «int».
        :param unread_count: Telegram type: «int».
        :param notify_settings: Telegram type: «PeerNotifySettings».
        :param pts: Telegram type: «int».
        :param draft: Telegram type: «DraftMessage».

        Constructor for Dialog: Instance of Dialog.
        """
        super().__init__()

        self.pinned = pinned
        self.peer = peer
        self.top_message = top_message
        self.read_inbox_max_id = read_inbox_max_id
        self.read_outbox_max_id = read_outbox_max_id
        self.unread_count = unread_count
        self.notify_settings = notify_settings
        self.pts = pts
        self.draft = draft

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

        self.peer.on_send(writer)
        writer.write_int(self.top_message)
        writer.write_int(self.read_inbox_max_id)
        writer.write_int(self.read_outbox_max_id)
        writer.write_int(self.unread_count)
        self.notify_settings.on_send(writer)
        if self.pts:
            writer.write_int(self.pts)

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

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

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

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

        self.peer = reader.tgread_object()
        self.top_message = reader.read_int()
        self.read_inbox_max_id = reader.read_int()
        self.read_outbox_max_id = reader.read_int()
        self.unread_count = reader.read_int()
        self.notify_settings = reader.tgread_object()
        if (flags & (1 << 0)) != 0:
            self.pts = reader.read_int()

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

    def __repr__(self):
        return 'dialog#66ffba14 flags:# pinned:flags.2?true peer:Peer top_message:int read_inbox_max_id:int read_outbox_max_id:int unread_count:int notify_settings:PeerNotifySettings pts:flags.0?int draft:flags.1?DraftMessage = Dialog'

    def __str__(self):
        return '(dialog (ID: 0x66ffba14) = (pinned={}, peer={}, top_message={}, read_inbox_max_id={}, read_outbox_max_id={}, unread_count={}, notify_settings={}, pts={}, draft={}))'.format(str(self.pinned), str(self.peer), str(self.top_message), str(self.read_inbox_max_id), str(self.read_outbox_max_id), str(self.unread_count), str(self.notify_settings), str(self.pts), str(self.draft))
