from ...tl.tlobject import TLObject


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

    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 to_dict(self):
        return {
            'pinned': self.pinned,
            'peer': None if self.peer is None else self.peer.to_dict(),
            'top_message': self.top_message,
            'read_inbox_max_id': self.read_inbox_max_id,
            'read_outbox_max_id': self.read_outbox_max_id,
            'unread_count': self.unread_count,
            'notify_settings': None if self.notify_settings is None else self.notify_settings.to_dict(),
            'pts': self.pts,
            'draft': None if self.draft is None else self.draft.to_dict(),
        }

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

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