# -*- coding: utf-8 -*-
# Author: Parrot Developers
# License: MPL 2.0 https://www.mozilla.org/en-US/MPL/2.0/

from .utils.resolve import Resolver, NotFound, EmptyPD
from .utils.defaultProgressDialog import defaultProgressDialog
from .plugins import *


def resolve(module, channel, username="", password="", data_file="./data.json", data_folder="", server_port=8080, progress_dialog=defaultProgressDialog):
    """
    Args:
        module (str): Plugin to use
        channel (str): Channel to grab
        username (str): Not required, some sources have defaults
        password (str): Not required, some sources have defaults
        data_file (str): Where to store data, default: ./data.json
        progressDialog (class): Gives info about grabbing progress

    Returns:
        rmf (ResolvedMediaFile): use .url or .headers to get data, .test() to test if stream works
    """

    if not all([module, channel]):
        raise Exception("Invalid arguments")

    # Filename / Classname in python cannot start with int
    if module[0].isdigit(): module = f"N{module}"

    return Resolver(data_file, data_folder, server_port, progress_dialog=progress_dialog).resolve(module, channel, username=username, password=password)



def preload(data_file="./data.json", data_folder="", server_port=8080):
    """
    Args:
        data_file (str): Where to store data, default: ./data.json

    Returns:
        None
    """


    return Resolver(data_file, data_folder, server_port).preloadResolvers()

def checkForGrabber(module):
    """
    Args:
        module (str): Name of module (ex. Nova)
    
    Returns:
        response (bool): True if exists, False if doesn't
    """

    resolver = Resolver(data_file=None,
                        data_folder=None,
                        server_port=None,
                        progress_dialog=EmptyPD)

    return resolver.module_exists(module)

