from .worker_template import TemplateWorker
from .download.browser import PDFDownloader
from .download.youtube import YouTubeDownloader
from .fetch.runner import get_description
from .upload.runner import upload_to_archive as run_upload
from .compress.runner import shrink_pdf

import time
import logging
logger = logging.getLogger(__name__)

class DownloadWorker(TemplateWorker):
    def __init__(self) -> None:
        self.dl_runner = PDFDownloader().download
        self.yt_runner = YouTubeDownloader().save_video
        super().__init__()

    def _handle_article(self, article_watcher):
        article = article_watcher.article
        u = article.article_url

        if "youtu.be/" in u or "youtube.com/" in u:
            action = self.yt_runner
        else:
            action = self.dl_runner

        super()._handle_article(article_watcher, action)
        article_watcher.download_completed = True



class FetchWorker(TemplateWorker):
    def __init__(self) -> None:
        super().__init__()

    def _handle_article(self, article_watcher):
        action = get_description # function
        super()._handle_article(article_watcher, action)
        article_watcher.fetch_completed = True



class UploadWorker(TemplateWorker):
    def __init__(self) -> None:
        super().__init__()

    def _handle_article(self, article_watcher):
        action = run_upload # function
        super()._handle_article(article_watcher, action)
        time.sleep(4) # Archive Uploads rate limited to 15/minute
        article_watcher.upload_completed = True



class CompressWorker(TemplateWorker):
    def __init__(self) -> None:
        super().__init__()

    def _handle_article(self, article_watcher):
        action = shrink_pdf
        super()._handle_article(article_watcher, action)
        article_watcher.compression_completed = True