from ...tl.mtproto_request import MTProtoRequest


class DraftMessage(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. TL definition below.
    draftMessage#fd8e711f flags:# no_webpage:flags.1?true reply_to_msg_id:flags.0?int message:string entities:flags.3?Vector<MessageEntity> date:date = DraftMessage"""

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

    def __init__(self, message, date, no_webpage=None, reply_to_msg_id=None, entities=None):
        """
        :param no_webpage: Telegram type: "true".
        :param reply_to_msg_id: Telegram type: "int".
        :param message: Telegram type: "string".
        :param entities: Telegram type: "MessageEntity". Must be a list.
        :param date: Telegram type: "date".

        Constructor for DraftMessage: Instance of either DraftMessageEmpty, DraftMessage.
        """
        super().__init__()

        self.no_webpage = no_webpage
        self.reply_to_msg_id = reply_to_msg_id
        self.message = message
        self.entities = entities
        self.date = date

    def to_dict(self):
        return {
            'no_webpage': self.no_webpage,
            'reply_to_msg_id': self.reply_to_msg_id,
            'message': self.message,
            'entities': [] if self.entities is None else [None if x is None else x.to_dict() for x in self.entities],
            'date': self.date,
        }

    def on_send(self, writer):
        writer.write_int(DraftMessage.constructor_id, signed=False)
        # Calculate the flags. This equals to those flag arguments which are NOT None
        flags = 0
        flags |= (1 << 1) if self.no_webpage else 0
        flags |= (1 << 0) if self.reply_to_msg_id else 0
        flags |= (1 << 3) if self.entities else 0
        writer.write_int(flags)

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

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

        writer.tgwrite_date(self.date)

    @staticmethod
    def empty():
        """Returns an "empty" instance (attributes=None)"""
        return DraftMessage(None, None, None, None, None)

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

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

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

        self.message = reader.tgread_string()
        if (flags & (1 << 3)) != 0:
            reader.read_int()  # Vector's constructor ID
            self.entities = []  # Initialize an empty list
            entities_len = reader.read_int()
            for _ in range(entities_len):
                entities_item = reader.tgread_object()
                self.entities.append(entities_item)

        self.date = reader.tgread_date()

    def __repr__(self):
        return 'draftMessage#fd8e711f flags:# no_webpage:flags.1?true reply_to_msg_id:flags.0?int message:string entities:flags.3?Vector<MessageEntity> date:date = DraftMessage'

    def __str__(self):
        return '(draftMessage (ID: 0xfd8e711f) = (no_webpage={}, reply_to_msg_id={}, message={}, entities={}, date={}))'.format(str(self.no_webpage), str(self.reply_to_msg_id), str(self.message), None if not self.entities else [str(_) for _ in self.entities], str(self.date))
