import pathlib
from typing import List


def find_all_detectors(rootdir: pathlib.Path,
                       prefix: str = 'SingleCell') -> List[str]:
    """ Find all the detectors under the root directory

    If your directories look like this:

    * ``/data/Experiments/2017-01-30/SingleCell-foo``
    * ``/data/Experiments/2017-01-30/SingleCell-bar``

    Then this will find detectors: ``['foo', 'bar']``

    :param Path rootdir:
        The experiment to load (e.g. '/data/Experiments/2017-01-30')
    :param str prefix:
        The prefix for subdirectories to look for detectors
    :returns:
        A list of detectors available for this experiment
    """
    rootdir = pathlib.Path(rootdir)
    if not rootdir.is_dir():
        raise OSError(f'Cannot find experiment: {rootdir}')
    detectors = []
    for p in rootdir.iterdir():
        if not p.name.startswith(prefix):
            continue
        if not p.is_dir():
            continue
        if '-' in p.name:
            detectors.append(p.name.split('-', 1)[1])
        else:
            detectors.append(None)
    if len(detectors) < 1:
        raise OSError(f'No {prefix} folders found under {rootdir}')
    return detectors