from ...tl.tlobject import TLObject


class InputMediaUploadedPhoto(TLObject):
    """Class generated by TLObjects' generator. All changes will be ERASED. TL definition below.
    inputMediaUploadedPhoto#2f37e231 flags:# file:InputFile caption:string stickers:flags.0?Vector<InputDocument> ttl_seconds:flags.1?int = InputMedia"""

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

    def __init__(self, file, caption, stickers=None, ttl_seconds=None):
        """
        :param file: Telegram type: "InputFile".
        :param caption: Telegram type: "string".
        :param stickers: Telegram type: "InputDocument". Must be a list.
        :param ttl_seconds: Telegram type: "int".

        Constructor for InputMedia: Instance of either InputMediaEmpty, InputMediaUploadedPhoto, InputMediaPhoto, InputMediaGeoPoint, InputMediaContact, InputMediaUploadedDocument, InputMediaDocument, InputMediaVenue, InputMediaGifExternal, InputMediaPhotoExternal, InputMediaDocumentExternal, InputMediaGame, InputMediaInvoice.
        """
        super().__init__()

        self.file = file
        self.caption = caption
        self.stickers = stickers
        self.ttl_seconds = ttl_seconds

    def to_dict(self):
        return {
            'file': None if self.file is None else self.file.to_dict(),
            'caption': self.caption,
            'stickers': [] if self.stickers is None else [None if x is None else x.to_dict() for x in self.stickers],
            'ttl_seconds': self.ttl_seconds,
        }

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

        self.file.on_send(writer)
        writer.tgwrite_string(self.caption)
        if self.stickers:
            writer.write_int(0x1cb5c415, signed=False)  # Vector's constructor ID
            writer.write_int(len(self.stickers))
            for _x in self.stickers:
                if _x:
                    _x.on_send(writer)

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

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

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

        self.file = reader.tgread_object()
        self.caption = reader.tgread_string()
        if (flags & (1 << 0)) != 0:
            reader.read_int()  # Vector's constructor ID
            self.stickers = []  # Initialize an empty list
            _len = reader.read_int()
            for _ in range(_len):
                _x = reader.tgread_object()
                self.stickers.append(_x)

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

    def __repr__(self):
        return 'inputMediaUploadedPhoto#2f37e231 flags:# file:InputFile caption:string stickers:flags.0?Vector<InputDocument> ttl_seconds:flags.1?int = InputMedia'

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

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