import os, logging
from typing import Dict, List, Optional

from theng.models import Model, PathfinderModel, FDSModel, VentusModel
from theng.args import get_arguments


_provided_models: dict = {
    "pathfinder": [],
    "pyrosim": [],
    "ventus": []
}

_known_file_extension_types: Dict[str, Model] = {
    ".pfr": PathfinderModel,
    ".smv": FDSModel,
    ".vnt": VentusModel
}

_LOGGER = logging.getLogger("theng")

def get_models() -> Dict[str, List[Model]]:
    """Get all models passed to the script by Results

    Returns:
        A Dictionary, keyed by the simulator name, containing all :py:class:`theng.models.Model` instances generated by data passed to the script.
        Keys are:
        
        - pathfinder
        - pyrosim
        - ventus (not currently supported)
    """ 
    get_pathfinder_models()
    get_pyrosim_models()
    # get_ventus_models()
    return _provided_models

def get_pathfinder_models() -> List[PathfinderModel]:
    """Gets all :py:class:`theng.models.PathfinderModel` instances generated by data passed to the script.

    Returns:
        A List of :py:class:`theng.models.PathfinderModel` instances generated by data passed to the script.
    """
    if (get_arguments().pathfinder != None):
        _provided_models["pathfinder"] = []
        for path in get_arguments().pathfinder:
            _provided_models["pathfinder"].append(_convert_to_model(path))
    return _provided_models["pathfinder"]

def get_pyrosim_models() -> List[FDSModel]:
    """Gets all :py:class:`theng.models.FDSModel` instances generated by data passed to the script.

    Returns:
        A List of :py:class:`theng.models.FDSModel` instances generated by data passed to the script.
    """
    if (get_arguments().pyrosim != None):
        _provided_models["pyrosim"] = []
        for path in get_arguments().pyrosim:
            _provided_models["pyrosim"].append(_convert_to_model(path))
    return _provided_models["pyrosim"]

def get_ventus_models() -> List[VentusModel]:
    """Ventus is not currently supported by theng.
    
    Gets all :py:class:`theng.models.VentusModel` instances generated by data passed to the script.

    Returns:
        A List of :py:class:`theng.models.VentusModel` instances generated by data passed to the script.
    """
    print("Ventus is not currently supported by theng")
    return []
    if (get_arguments().ventus != None):
        _provided_models["ventus"] = []
        for path in get_arguments().ventus:
            _provided_models["ventus"].append(_convert_to_model(path))
    return _provided_models["ventus"]
            
def _convert_to_model(results_file_name: str) -> Optional[Model]:
    """Factory to convert a results file (.pfr, .smv, .vnt) to an appropriate Model object
    
    Returns:
        A Model object instance if the results file has a valid associated model.
        None if otherwise, or if the extension is unknown.
    """
    ext: str = os.path.splitext(results_file_name)[-1]
    
    try:
        if ext in _known_file_extension_types.keys():
            return _known_file_extension_types[ext](results_file_name)
        return None
    except ValueError as e:
        _LOGGER.critical(f'{results_file_name} is not a supported results file type')
        return None     