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 = [
    "format",
    "format_column",
    "labels_column",
    "format_row",
    "labels_row",
    "plot",
    "title",
    "line_color",
    "line_marker",
    "line_marker_text",
    "line_marker_size",
    "line_marker_opacity",
    "line_marker_scale",
    "line_error_bars",
    "line_style",
    "line_width",
    "stacked",
    "xaxis",
    "yaxis",
    "palette",
    "palette_position",
    "palette_range",
    "palette_show",
    "palette_title",
    "width",
    "height",
    "show_legend",
    "legend_position",
    "show_legend_border",
    "show_border",
    "plot_margins",
    "plot_title",
    "plot_xaxis_type",
    "plot_yaxis_type",
    "xrange",
    "yrange",
    "xaxis_format",
    "yaxis_format",
    "xtitle",
    "ytitle",
    "item_justification",
    "nan_display",
    "table_sort",
    "table_title",
    "align_column",
    "table_search",
    "table_page",
    "table_pagemenu",
    "table_scrollx",
    "table_scrolly",
    "table_bordered",
    "table_condensed",
    "table_wrap_content",
    "table_default_col_labels",
    "table_cond_format",
    "column_minimum",
    "column_maximum"
]


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.ERROR)
    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.ERROR)
    formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
    ch.setFormatter(formatter)
    logger.addHandler(ch)
    return logger
