"""LICENSE
Copyright 2020 Hermann Krumrey <hermann@krumreyh.com>

This file is part of otaku-info.

otaku-info is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

otaku-info 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with otaku-info.  If not, see <http://www.gnu.org/licenses/>.
LICENSE"""

from typing import Optional, Dict, Any, Tuple
from otaku_info.enums import MediaType, MediaSubType, \
    ReleasingState, MediaRelationType, ListService


class AnimeListItem:
    """
    Class that models a general anime list item
    """
    def __init__(
            self,
            _id: int,
            service: ListService,
            extra_ids: Dict[ListService, str],
            media_type: MediaType,
            media_subtype: MediaSubType,
            english_title: Optional[str],
            romaji_title: str,
            cover_url: str,
            chapters: Optional[int],
            volumes: Optional[int],
            episodes: Optional[int],
            releasing_state: ReleasingState,
            relations: Dict[Tuple[MediaType, int], MediaRelationType]
    ):
        """
        Initializes the AnimeListItem object
        :param _id: The anime list ID
        :param service: The anime list service
        :param extra_ids: Any extra IDs for other services
        :param media_type: The media type of the series
        :param media_subtype: The media subtype of the series
        :param english_title: The English title of the series
        :param romaji_title: The Japanes title of the series written in romaji
        :param cover_url: URL to a cover image for the series
        :param chapters: The total amount of known manga chapters
        :param volumes: The total amount of known manga/ln volumes
        :param episodes: The total amount of known anime episodes
        :param releasing_state: The current releasing state of the series
        :param relations: Related media items identified by IDs
        """
        self.id = _id
        self.service = service
        self.extra_ids = extra_ids
        self.media_type = media_type
        self.media_subtype = media_subtype
        self.english_title = english_title
        self.romaji_title = romaji_title
        self.cover_url = cover_url
        self.chapters = chapters
        self.volumes = volumes
        self.episodes = episodes
        self.releasing_state = releasing_state
        self.relations = relations

    @property
    def latest_release(self) -> Optional[int]:
        """
        :return: The latest release. Chapters for manga, episodes for anime
        """
        if self.media_type == MediaType.ANIME:
            return self.episodes
        else:
            return self.chapters

    @classmethod
    def from_query(cls, media_type: MediaType, data: Dict[str, Any]) \
            -> "AnimeListItem":
        """
        Generates an AnimeListItem from a dictionary generated by an APi query
        :param media_type: The media type of the item
        :param data: The data to use
        :return: The generated AnimeListItem
        """
        raise NotImplementedError()
