from .base import BaseDownloader

# Import from new organized structure
from .video_players import (
    BaseVideoPlayer,
    get_video_player,
    DoodStreamDownloader,
    SibnetDownloader,
    VidMolyDownloader,
    SendVidDownloader,
    LpayerDownloader,
    UnFichierDownloader,
    UptoboxDownloader,
    RapidFileDownloader
)
from .anime_sites import (
    BaseAnimeSite,
    get_anime_site,
    AnimeSamaDownloader,
    NekoSamaDownloader,
    AnimeUltimeDownloader,
    VostfreeDownloader
)
from .series_sites import (
    BaseSeriesSite,
    get_series_site,
    FS7Downloader,
    ZoneTelechargementDownloader
)


def get_downloader(url: str) -> BaseDownloader:
    """
    Factory function to get the appropriate downloader for a URL.

    This function now uses the organized structure:
    - Checks anime sites first (for catalogs/search)
    - Then checks series sites (for catalogs/search)
    - Then checks video players (for direct download links)
    - Falls back to generic downloader if no match
    """
    # Try anime sites first
    anime_site = get_anime_site(url)
    if anime_site:
        return anime_site

    # Then try series sites
    series_site = get_series_site(url)
    if series_site:
        return series_site

    # Then try video players
    video_player = get_video_player(url)
    if video_player:
        return video_player

    # Return generic downloader if no match
    return GenericDownloader()


class GenericDownloader(BaseDownloader):
    """Generic downloader for unhandled hosts"""

    def can_handle(self, url: str) -> bool:
        return True

    async def get_download_link(self, url: str, target_filename: str = None) -> tuple[str, str]:
        # Just return the URL as-is
        filename = target_filename or url.split('/')[-1] or "download"
        return url, filename
