import errno
import os

from loguru import logger
from yt_dlp import download_range_func

from src.core.config import DEFAULT_DURATION
from src.core.ydl import VideoDownloader
from src.exceptions.download_exceptions import FileAlreadyExistException


class BaseParser:
    BASE_ENCODING = 'utf-8'
    BASE_DIR = None

    def __init__(self, params: dict):
        self.params = params

    def video_download(self):
        ydl_opts = {
            "format": self.params["format"],
            "logger": logger,
            "merge_output_format": self.params["merge_output_format"],
            'outtmpl': self.params["outtmpl"],
            'download_ranges': download_range_func(None, [(0, int(DEFAULT_DURATION))]),
            # "quiet": True
        }
        downloader = VideoDownloader(link=self.params["link"], ydl_opts=ydl_opts)
        downloader.get_info()

        if 'resolution' in downloader.info and downloader.info["resolution"]:
            resolution = downloader.info['resolution']
        else:
            resolution = "NA"

        base_file_name = f"{downloader.info['id']}_{resolution}.{downloader.info['ext']}"
        if "Yahoo" in ydl_opts["outtmpl"]["default"]:
            path_to_video = f"Yahoo/{base_file_name}"
        elif "ZenYandex" in ydl_opts["outtmpl"]["default"]:
            path_to_video = f"ZenYandex/{base_file_name}"
        elif "Bing" in ydl_opts["outtmpl"]["default"]:
            path_to_video = f"Bing/{base_file_name}"
        else:
            path_to_video = f"{downloader.info['extractor_key']}/{base_file_name}"
        if os.path.exists(os.path.join(os.getcwd() + "/downloads/" + path_to_video)):
            raise FileAlreadyExistException(message=path_to_video)
        downloader.ydl_opts["quiet"] = False
        downloader.download()
        return path_to_video

    def make_sure_path_exists(self,):
        try:
            os.makedirs(self.BASE_DIR)
        except OSError as exception:
            if exception.errno != errno.EEXIST:
                raise
            
    '''
    TODO: скорее всего добавить процедуру для конвертации итогого файла через ffmpeg, используя
    '''
