#  Pyrogram - Telegram MTProto API Client Library for Python
#  Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
#  This file is part of Pyrogram.
#
#  Pyrogram is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Pyrogram is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public License
#  along with Pyrogram.  If not, see <http://www.gnu.org/licenses/>.

import logging
from datetime import datetime

import pyrogram
from pyrogram import raw, utils
from pyrogram import types
from pyrogram.file_id import FileId, FileType, FileUniqueId, FileUniqueType, ThumbnailSource
from ..object import Object

log = logging.getLogger(__name__)


class Photo(Object):
    """A Photo.

    Parameters:
        sizes (List of :obj:`~pyrogram.types.Thumbnail`):
            Available variants of the photo, in different sizes.

        date (:py:obj:`~datetime.datetime`):
            Date the photo was sent.

        ttl_seconds (``int``, *optional*):
            Time-to-live seconds, for secret photos.

        thumbs (List of :obj:`~pyrogram.types.Thumbnail`, *optional*):
            Available thumbnails of this photo.

    """

    def __init__(
        self,
        *,
        client: "pyrogram.Client" = None,
        sizes: list["types.Thumbnail"],
        date: datetime,
        ttl_seconds: int = None,
        has_spoiler: bool = None,
        thumbs: list["types.Thumbnail"] = None
    ):
        super().__init__(client)

        self.sizes = sizes
        self.date = date
        self.ttl_seconds = ttl_seconds
        self.has_spoiler = has_spoiler
        self.thumbs = thumbs

    @staticmethod
    def _parse(
        client,
        photo: "raw.types.Photo",
        ttl_seconds: int = None,
        has_spoiler: bool = None,
    ) -> "Photo":
        if isinstance(photo, raw.types.Photo):
            photos: list[raw.types.PhotoSize] = []

            for p in photo.sizes:
                if isinstance(p, raw.types.PhotoSize):
                    photos.append(p)

                if isinstance(p, raw.types.PhotoSizeProgressive):
                    photos.append(
                        raw.types.PhotoSize(
                            type=p.type,
                            w=p.w,
                            h=p.h,
                            size=max(p.sizes)
                        )
                    )

            photos.sort(key=lambda p: p.size)

            return Photo(
                sizes=[
                    types.Thumbnail(
                        file_id=FileId(
                            file_type=FileType.PHOTO,
                            dc_id=photo.dc_id,
                            media_id=photo.id,
                            access_hash=photo.access_hash,
                            file_reference=photo.file_reference,
                            thumbnail_source=ThumbnailSource.THUMBNAIL,
                            thumbnail_file_type=FileType.PHOTO,
                            thumbnail_size=main.type,
                            volume_id=0,
                            local_id=0
                        ).encode(),
                        file_unique_id=FileUniqueId(
                            file_unique_type=FileUniqueType.DOCUMENT,
                            media_id=photo.id
                        ).encode(),
                        width=main.w,
                        height=main.h,
                        file_size=main.size,
                    )
                    for main in photos
                ],
                date=utils.timestamp_to_datetime(photo.date),
                ttl_seconds=ttl_seconds,
                has_spoiler=has_spoiler,
                thumbs=types.Thumbnail._parse(client, photo),
                client=client
            )

    @property
    def file_id(self) -> str:
        log.warning(
            "This property is deprecated. "
            "Please use sizes instead"
        )
        if len(self.sizes) > 0:
            return self.sizes[-1].file_id
        return None

    @property
    def file_unique_id(self) -> str:
        log.warning(
            "This property is deprecated. "
            "Please use sizes instead"
        )
        if len(self.sizes) > 0:
            return self.sizes[-1].file_unique_id
        return None

    @property
    def width(self) -> int:
        log.warning(
            "This property is deprecated. "
            "Please use sizes instead"
        )
        if len(self.sizes) > 0:
            return self.sizes[-1].width
        return None

    @property
    def height(self) -> int:
        log.warning(
            "This property is deprecated. "
            "Please use sizes instead"
        )
        if len(self.sizes) > 0:
            return self.sizes[-1].height
        return None

    @property
    def file_size(self) -> int:
        log.warning(
            "This property is deprecated. "
            "Please use sizes instead"
        )
        if len(self.sizes) > 0:
            return self.sizes[-1].file_size
        return None
