from ...tl.tlobject import TLObject


class MessageMediaPhoto(TLObject):
    """Class generated by TLObjects' generator. All changes will be ERASED. TL definition below.
    messageMediaPhoto#b5223b0f flags:# photo:flags.0?Photo caption:flags.1?string ttl_seconds:flags.2?int = MessageMedia"""

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

    def __init__(self, photo=None, caption=None, ttl_seconds=None):
        """
        :param photo: Telegram type: "Photo".
        :param caption: Telegram type: "string".
        :param ttl_seconds: Telegram type: "int".

        Constructor for MessageMedia: Instance of either MessageMediaEmpty, MessageMediaPhoto, MessageMediaGeo, MessageMediaContact, MessageMediaUnsupported, MessageMediaDocument, MessageMediaWebPage, MessageMediaVenue, MessageMediaGame, MessageMediaInvoice.
        """
        super().__init__()

        self.photo = photo
        self.caption = caption
        self.ttl_seconds = ttl_seconds

    def to_dict(self):
        return {
            'photo': None if self.photo is None else self.photo.to_dict(),
            'caption': self.caption,
            'ttl_seconds': self.ttl_seconds,
        }

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

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

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

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

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

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

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

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

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

    def __repr__(self):
        return 'messageMediaPhoto#b5223b0f flags:# photo:flags.0?Photo caption:flags.1?string ttl_seconds:flags.2?int = MessageMedia'

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

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