"""
The tool to check the availability or syntax of domain, IP or URL.

::


    ██████╗ ██╗   ██╗███████╗██╗   ██╗███╗   ██╗ ██████╗███████╗██████╗ ██╗     ███████╗
    ██╔══██╗╚██╗ ██╔╝██╔════╝██║   ██║████╗  ██║██╔════╝██╔════╝██╔══██╗██║     ██╔════╝
    ██████╔╝ ╚████╔╝ █████╗  ██║   ██║██╔██╗ ██║██║     █████╗  ██████╔╝██║     █████╗
    ██╔═══╝   ╚██╔╝  ██╔══╝  ██║   ██║██║╚██╗██║██║     ██╔══╝  ██╔══██╗██║     ██╔══╝
    ██║        ██║   ██║     ╚██████╔╝██║ ╚████║╚██████╗███████╗██████╔╝███████╗███████╗
    ╚═╝        ╚═╝   ╚═╝      ╚═════╝ ╚═╝  ╚═══╝ ╚═════╝╚══════╝╚═════╝ ╚══════╝╚══════╝

Provides the base of all our downloader.

Author:
    Nissar Chababy, @funilrys, contactTATAfunilrysTODTODcom

Special thanks:
    https://pyfunceble.github.io/#/special-thanks

Contributors:
    https://pyfunceble.github.io/#/contributors

Project link:
    https://github.com/funilrys/PyFunceble

Project documentation:
    https://docs.pyfunceble.com

Project homepage:
    https://pyfunceble.github.io/

License:
::


    Copyright 2017, 2018, 2019, 2020, 2022, 2023, 2024, 2025 Nissar Chababy

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        https://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""

import datetime
import os
from typing import Optional

import PyFunceble.downloader.exceptions
import PyFunceble.exceptions
import PyFunceble.storage
from PyFunceble.helpers.dict import DictHelper
from PyFunceble.helpers.download import DownloadHelper
from PyFunceble.helpers.file import FileHelper


class DownloaderBase:
    """
    Provides the base of all downloader classes.

    The interface is actually simple, but the part which may be hard to
    understand is the "downtime" part. What we do, is that we save the
    download time inside a JSON file, so this class provides the base around
    the download mechanism but also the generation or update of that JSON file.
    """

    DOWNTIME_INDEX: Optional[str] = None
    """
    Used to set/track the download time of the current file.
    """

    DOWNLOAD_FREQUENCY: int = 1
    """
    The download frequency (in day).

    Example:
        if 1 is given, it's once every 24 hours.

    .. warning::
        A frequency of :code:`0` or a negative number will force the download
        every hour.
    """

    DEFAULT_DOWNLOAD_URL: Optional[str] = None
    """
    The URL to download.
    """

    DEFAULT_FILENAME: Optional[str] = None
    """
    The name of the file to download.
    """

    all_downtimes: Optional[dict] = {}
    """
    Stores the download time of all files (self managed).
    """

    _config_dir: Optional[str] = None
    _destination: Optional[str] = None
    _download_link: Optional[str] = None
    _download_helper: Optional[DownloadHelper] = None

    dict_helper: Optional[DictHelper] = None

    def __init__(self, *, config_dir: Optional[str] = None) -> None:
        self.dict_helper = DictHelper()

        if config_dir is not None:
            self.config_dir = config_dir
        else:
            self.config_dir = PyFunceble.storage.CONFIG_DIRECTORY

        self.downtimes_file = FileHelper(
            os.path.join(self.config_dir, ".pyfunceble_intern_downtime.json")
        )

        self.all_downtimes.update(self.get_all_downtimes())

        if self.DEFAULT_DOWNLOAD_URL is not None:
            self.download_link = self.DEFAULT_DOWNLOAD_URL

        if self.DEFAULT_FILENAME is not None:
            self.destination = os.path.join(self.config_dir, self.DEFAULT_FILENAME)

        self._download_helper = DownloadHelper(
            self.download_link,
            own_proxy_handler=True,
            proxies=PyFunceble.storage.PROXY,
            certificate_validation=(
                PyFunceble.storage.CONFIGURATION.verify_ssl_certificate
                if PyFunceble.storage.CONFIGURATION
                else True
            ),
        )

    @property
    def authorized(self) -> bool:
        """
        Provides the authorization to start the download.
        """

        raise NotImplementedError()

    @property
    def config_dir(self) -> Optional[str]:
        """
        Provides the current state of the :code:`_config_dir` attribute.
        """

        return self._config_dir

    @config_dir.setter
    def config_dir(self, value: str) -> None:
        """
        Sets the configuration directory.

        :param value:
            The value to set.

        :raise TypeError:
            When value is not a :py:class:`str`.
        """

        if not isinstance(value, str):
            raise TypeError(f"<value> should be {str}, {type(value)} given.")

        self._config_dir = value

    def set_config_dir(self, value: str) -> "DownloaderBase":
        """
        Sets the configuration directory.

        :param value:
            The value to set.
        """

        self.config_dir = value

        return self

    @property
    def destination(self) -> Optional[str]:
        """
        Provides the current state of the :code:`_destination` attribute.
        """

        return self._destination

    @destination.setter
    def destination(self, value: str) -> None:
        """
        Sets the destination.

        :param value:
            The value to set.

        :raise TypeError:
            When value is not a :py:class:`str`.
        """

        if not isinstance(value, str):
            raise TypeError(f"<value> should be {str}, {type(value)} given.")

        self._destination = value

    def set_destination(self, value: str) -> "DownloaderBase":
        """
        Sets the destination.

        :param value:
            The value to set.
        """

        self.destination = value

        return self

    @property
    def download_link(self) -> Optional[str]:
        """
        Provides the current state of the :code:`_download_link` attribute.
        """

        return self._download_link

    @download_link.setter
    def download_link(self, value: str) -> None:
        """
        Sets the link to download.

        :param value:
            The value to set.

        :raise TypeError:
            When value is not a :py:class:`str`.
        """

        if not isinstance(value, str):
            raise TypeError(f"<value> should be {str}, {type(value)} given.")

        self._download_link = value

    def set_download_link(self, value: str) -> "DownloaderBase":
        """
        Sets the link to download.

        :param value:
            The value to set.
        """

        self.download_link = value

        return self

    def get_all_downtimes(self) -> dict:
        """
        Provides the downloadtime of all files.
        """

        return self.dict_helper.from_json_file(self.downtimes_file.path)

    def is_downtime_set(self) -> bool:
        """
        Checks if the download time of the current object exists.
        """

        return (
            self.DOWNTIME_INDEX in self.all_downtimes
            and self.all_downtimes[self.DOWNTIME_INDEX]
            and all(
                x in self.all_downtimes[self.DOWNTIME_INDEX]
                and self.all_downtimes[self.DOWNTIME_INDEX][x]
                for x in ["iso", "timestamp"]
            )
        )

    def get_current_downtime(self):
        """
        Provides the download times of the current :code:`DOWN_TIME_INDEX`.
        """

        if self.is_downtime_set():
            return self.all_downtimes[self.DOWNTIME_INDEX]
        return None

    def set_current_downtime(self) -> "DownloaderBase":
        """
        Sets the current datetime into our registry.
        """

        current_datetime = datetime.datetime.now(datetime.timezone.utc)
        if epoch := os.environ.get("SOURCE_DATE_EPOCH"):
            current_datetime = datetime.datetime.fromtimestamp(
                float(epoch), datetime.timezone.utc
            )

        self.all_downtimes[self.DOWNTIME_INDEX] = {
            "iso": current_datetime.isoformat(),
            "timestamp": current_datetime.timestamp(),
        }

        return self

    def save_all_downtimes(self) -> None:
        """
        Saves the current state of the all downtimes.
        """

        self.dict_helper.set_subject(self.all_downtimes).to_json_file(
            self.downtimes_file.path
        )

    def is_last_download_expired(self) -> bool:
        """
        Checks if the last downloaded file is expired (if exists).
        """

        if not FileHelper(self.destination).exists() or not self.is_downtime_set():
            return True

        last_downloaded_time = datetime.datetime.fromtimestamp(
            self.get_current_downtime()["timestamp"], datetime.timezone.utc
        )

        if (
            self.DOWNLOAD_FREQUENCY <= 0
            and (
                datetime.datetime.now(datetime.timezone.utc) - last_downloaded_time
            ).seconds
            < 3600
        ):
            return False

        if last_downloaded_time + datetime.timedelta(
            days=self.DOWNLOAD_FREQUENCY
        ) <= datetime.datetime.now(datetime.timezone.utc):
            return True

        return False

    def start(self) -> None:
        """
        Starts the download process.
        """

        if self.authorized and self.is_last_download_expired():
            if not hasattr(self, "destination") or not self.destination:
                raise PyFunceble.downloader.exceptions.NoDownloadDestinationGiven()

            if not hasattr(self, "download_link") or not self.download_link:
                raise PyFunceble.downloader.exceptions.NoDownloadLinkGiven()

            if self._download_helper.download_text(destination=self.destination):
                self.set_current_downtime()
                self.save_all_downtimes()
