import logging
import textwrap


def class_for_config(base_class, config):
    """
    Returns the feature class for a given config dictionary or Scooch Config object.
    This class search is performed under the assumption that each key in the dictionary
    or Config is a class in the pandafeet features module.

    Args:
        base_class: object - The base class for which you want to search for derived 
        classes of.

        config: dict - A dictionary with the keys specifying class names in the pandafeet
        features module.

    Return:
        class - The class that can be constructed with the provided config dictionary.
    """
    fclsses = base_class._all_subclasses() + [base_class]
    feature_class_names = [clsobj.__name__ for clsobj in fclsses]

    # Search for matching configuration
    config_fields = config.keys()
    matching_classes = [c_field for c_field in config_fields if c_field in feature_class_names]
    if not len(matching_classes):
        logging.getLogger().error(textwrap.dedent(f"""
                        Provided configuration does not match any, or matches multiple classes in the provided class hierarchy
                        Candidates were: {str(feature_class_names)}
                        Config requested: {str(list(config_fields))}
                        """))
        raise KeyError("""Scooch configuration does not match any, or matches multiple classes in the provided class hierarchy""")

    # Get all matching classes
    f_classes = [fclsses[feature_class_names.index(match_cls)] for match_cls in matching_classes]

    # Return one class if there is only one
    if len(f_classes) == 1:
        return f_classes[0]

    # Otherwise return the list of classes
    return f_classes