from __future__ import annotations

from datetime import datetime, date
from typing import Optional, List, Iterator

from weasel_client.resources.Resource import Resource


class Vulnerability(Resource):
    """
    Resource class for Vulnerabilities
    """
    _cve_number: str
    _publish_date: str
    _last_modified: str
    _title: Optional[str]
    _type: Optional[str]
    _references: Optional[str]
    _fishy: bool

    def __init__(
            self,
            APIClient,
            id: int,
            url: str,
            cve_number: str,
            publish_date: str,
            last_modified: str,
            fishy: bool,
            title: Optional[str] = None,
            type: Optional[str] = None,
            references: Optional[str] = None,

    ):
        super().__init__(APIClient=APIClient, id=id, url=url)
        self._cve_number = cve_number
        self._publish_date = publish_date
        self._last_modified = last_modified
        self._title = title
        self._type = type
        self._references = references
        self._fishy = fishy

    def cve_number(self) -> str:
        """
        Returns the CVE Number
        """
        return self._cve_number

    def publish_date(self) -> date:
        """
        Returns the publish date
        """
        return date.fromisoformat(self._publish_date)

    def last_modified(self) -> datetime:
        """
        Returns the time the vulnerability was last modified
        """
        return datetime.fromisoformat(self._last_modified)

    def title(self) -> Optional[str]:
        """
        Returns the title
        """
        return self._title

    def type(self) -> Optional[str]:
        """
        Returns the vulnerability type
        """
        return self._type

    def references(self) -> Optional[List[str]]:
        """
        # Todo
        Should return the vulnerability references
        """
        if self._references is None:
            return None
        else:
            return None

    def fishy(self) -> bool:
        """
        Returns the fishy-flag
        """
        return self._fishy

    def found_in(self) -> Iterator[Resource]:
        """
        Returns the associated releases the vulnerability was found in
        """
        return self._client.vulnerability_found_in(pk=self._id)

    def fixed_by(self) -> Iterator[Resource]:
        """
        Returns the associated releases the vulnerability was fixed by
        """
        return self._client.vulnerability_fixed_by(pk=self._id)

    @staticmethod
    def from_id(APIClient, id: int) -> Optional[Vulnerability]:
        """
        Fetches a vulnerability-object with a given ID
        :param APIClient: client for API-requests
        :param id: id of the object to fetch
        """
        return APIClient.vulnerability_detail(pk=id)
