"""Module to parse arguments passed by Thunderhead Results"""
import argparse
import sys, os
from typing import Optional

arguments: Optional[argparse.Namespace] = None
_argument_parser : argparse.ArgumentParser | None = None

if 'sphinx' not in sys.argv[0]:
    _argument_parser : argparse.ArgumentParser = argparse.ArgumentParser(add_help=False)

    _argument_parser.add_argument('--path', nargs="+", dest="pathfinder", help="Names of Pathfinder Results .pfr files")
    _argument_parser.add_argument('--fds', nargs="+", dest="pyrosim", help="Names of PyroSim Results .smv files")
    _argument_parser.add_argument("--ventus", nargs="+", dest="ventus", help="Names of Ventus Results files")
    _argument_parser.add_argument('-q', '--quiet', action="store_true", dest="quiet", help="Suppresses low level built-in warnings of the package")
    _argument_parser.add_argument('-h', '--help', action='help', default='==SUPPRESS==', help=('show this help message and exit'))

    arguments = _argument_parser.parse_known_args()[0]
    
def add_model_paths(*paths):
    """Adds additional paths to the list of arguments for later parsing by the package
    
    Arguments:
        paths (List[str]): Series or list of paths to add to the arguments
    
    Returns:
        The parsed arguments generated by the arguments parser
    """
    pfr = []
    smv = []
    vnt = []
    
    for path in paths:
        
        # This allows for a list of paths to be provided to the functions
        items = []
        if isinstance(path, list):
            items = [item for item in path]
        else:
            items = [path]
        for item in items:
            match os.path.splitext(item)[1]:
                case ".pfr":
                    pfr.append(item)
                case ".smv":
                    smv.append(item)
                case ".vnt":
                    vnt.append(item)
                
    if pfr != []:
        sys.argv.extend(["--path"])
        sys.argv.extend(x for x in pfr)
    if smv != []:
        sys.argv.extend(["--fds"])
        sys.argv.extend(x for x in smv)
    if vnt != []:
        sys.argv.extend(["--ventus"])
        sys.argv.extend(x for x in vnt)
        
    global arguments
    arguments = _argument_parser.parse_known_args()[0]
    return arguments

def suppress_warnings():
    sys.argv.extend(["-q"])
    global arguments
    arguments = _argument_parser.parse_known_args()[0]
    return arguments

def get_arguments():
    global arguments
    return arguments