import logging
import sys

# Dictionary to match:
# key: ADR DataItem type
# value: ADR item_* field
dict_items = {
    "animation": "item_animation",
    "file": "item_file",
    "html": "item_text",
    "image": "item_image",
    "string": "item_text",
    "scene": "item_scene",
    "table": "item_table",
    "tree": "item_tree",
}

# Dictionary to match:
# key: ADR item_* field
# value: ADR DataItem type
# (inverse of dict_items
type_maps = {
    "item_text": "text",
    "item_scene": "scene",
    "item_image": "image",
    "item_table": "table",
    "item_animation": "animation",
    "item_file": "file",
    "item_tree": "tree",
}

# Table attributes. To be generated by the read_prop.py file
table_attr = [
]


def in_ipynb():
    try:
        ipy_str = str(type(get_ipython()))
        if "zmqshell" in ipy_str:
            return True
        if "terminal" in ipy_str:
            return False
    except Exception:  # todo: please specify the possible exceptions here.
        return False


def get_logger(logfile=None):
    """
    Create a logger for ``pydynamicreporting`` if it does not exist already.

    Parameters
    ----------
        logfile: str
            Name of the file for the log. If none, then use default stream

    Return
    ------
        logger: logging.logger
            The logger object.
    """
    logger = logging.getLogger()
    logger.setLevel(logging.DEBUG)
    if logfile is None:
        # Logging for Python APIs should be disabled by default
        ch = logging.NullHandler()
    elif logfile=='stdout':
        ch = logging.StreamHandler(sys.stdout)
    else:
        ch = logging.FileHandler(logfile)
    ch.setLevel(logging.DEBUG)
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    return logger
