from ...tl.mtproto_request import MTProtoRequest


class Photo(MTProtoRequest):
    """Class generated by TLObjects' generator. All changes will be ERASED. Original .tl definition below.
    photo#9288dd29 flags:# has_stickers:flags.0?true id:long access_hash:long date:date sizes:Vector<PhotoSize> = Photo"""

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

    def __init__(self, id, access_hash, date, sizes, has_stickers=None):
        """
        :param has_stickers: Telegram type: «true».
        :param id: Telegram type: «long».
        :param access_hash: Telegram type: «long».
        :param date: Telegram type: «date».
        :param sizes: Telegram type: «PhotoSize». Must be a list.

        Constructor for Photo: Instance of either PhotoEmpty, Photo.
        """
        super().__init__()

        self.has_stickers = has_stickers
        self.id = id
        self.access_hash = access_hash
        self.date = date
        self.sizes = sizes

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

        writer.write_long(self.id)
        writer.write_long(self.access_hash)
        writer.tgwrite_date(self.date)
        writer.write_int(0x1cb5c415, signed=False)  # Vector's constructor ID
        writer.write_int(len(self.sizes))
        for sizes_item in self.sizes:
            sizes_item.on_send(writer)

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

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

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

        self.id = reader.read_long()
        self.access_hash = reader.read_long()
        self.date = reader.tgread_date()
        reader.read_int()  # Vector's constructor ID
        self.sizes = []  # Initialize an empty list
        sizes_len = reader.read_int()
        for _ in range(sizes_len):
            sizes_item = reader.tgread_object()
            self.sizes.append(sizes_item)

    def __repr__(self):
        return 'photo#9288dd29 flags:# has_stickers:flags.0?true id:long access_hash:long date:date sizes:Vector<PhotoSize> = Photo'

    def __str__(self):
        return '(photo (ID: 0x9288dd29) = (has_stickers={}, id={}, access_hash={}, date={}, sizes={}))'.format(str(self.has_stickers), str(self.id), str(self.access_hash), str(self.date), None if not self.sizes else [str(_) for _ in self.sizes])
