from yta_stock_downloader import StockVideoDownloader
from yta_general_utils.programming.parameter_validator import PythonValidator
from yta_general_utils.programming.decorators import singleton
from yta_general_utils.file.enums import FileTypeX
from yta_general_utils.programming.output import Output
from typing import Union


@singleton
class StockDownloader:
    """
    Singleton class.

    This object simplifies the access to our Stock provider platforms.
    It uses our stock library to connecto with different stock 
    platforms directly to download the content.

    This object is used to download stock videos to build the 
    main images that are shown in the videos.
    """

    _do_ignore_repeated: bool = True
    """
    Flag that indicates if a repeated stock video
    must be ignored and use another different
    video instead, or not.
    """

    def __init__(
        self,
        _do_ignore_repeated: bool = True
    ):
        if not hasattr(self, '_do_ignore_repeated'):
            self._do_ignore_repeated = _do_ignore_repeated

        # TODO: Refactor  this when confirmed that is working
        self._video_downloader = StockVideoDownloader(do_ignore_ids = _do_ignore_repeated)

    def download_video(
        self,
        keywords: str,
        do_randomize: bool = False,
        output_filename: Union[str, None] = None
    ) -> Union[str, None]:
        """
        Searches the provided 'keywords' in the different
        stock platforms we have looking for videos. This
        method will download, if any video is found, a video
        that could be the first one or a random one if
        'do_randomize' is True.

        This method will return the locally stored filename
        when downloaded, or None if no video is found.
        """
        if not PythonValidator.is_string(keywords):
            raise Exception('The "keywords" parameter provided is not a valid string.')
        
        if not PythonValidator.is_boolean(do_randomize):
            raise Exception('The "do_ranomize" parameter provided is not a boolean.')
        
        download_method = (
            lambda query, output_filename: self._video_downloader.download_random(
                query = query,
                output_filename = output_filename
            )
            if do_randomize else
            lambda query, output_filename: self._video_downloader.download(
                query = query,
                output_filename = output_filename
            )
        )

        # TODO: What about the sound (?)
        try:
            video = download_method(
                query = keywords,
                output_filename = Output.get_filename(output_filename, FileTypeX.VIDEO)
            )
        except:
            pass

        # TODO: Remove this code below if above is working
        # # TODO: What about the sound?
        # if do_randomize:
        #     try:
        #         video = self._video_downloader.download_random(
        #             query = keywords,
        #             output_filename = output_filename
        #         )
        #     except:
        #         pass
        # else:
        #     try:
        #         video = self._video_downloader.download(
        #             query = keywords,
        #             output_filename = output_filename
        #         )
        #     except:
        #         pass

        # TODO: Should I return the FileReturn instead (?)
        return (
            video.filename
            if video is not None else
            None
        )
    