"""
Set the colorscheme for matplotlib plots and latex.

Calling this script generates util/colorscheme.tex containing xcolor definitions.
"""
import matplotlib as mpl
import matplotlib.pyplot as plt
from cycler import cycler

# default order for matplotlib
color_order = ["blue", "orange", "green", "red", "purple", "yellow", "aqua", "gray"]

def set_mpl_colorscheme(palette: dict[str, str]):
    P = palette
    FIG_BG_COLOR =          P["bg0"]
    PLT_FG_COLOR =          P["fg0"]
    PLT_BG_COLOR =          P["bg0"]
    PLT_GRID_COLOR =        P["bg2"]
    LEGEND_FG_COLOR =       PLT_FG_COLOR
    LEGEND_BG_COLOR =       P["bg1"]
    LEGEND_BORDER_COLOR =   P["bg2"]
    COLORS = [P[f"fg-{c}"] for c in color_order]


    color_rcParams = {
        'axes.edgecolor': PLT_FG_COLOR,
        'axes.facecolor': PLT_BG_COLOR,
        'axes.labelcolor': PLT_FG_COLOR,
        'axes.titlecolor': 'auto',
        # 'axes.prop_cycle': cycler('color', ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']),
        'axes.prop_cycle': cycler('color', COLORS),
        # 'axes3d.xaxis.panecolor': (0.95, 0.95, 0.95, 0.5),
        # 'axes3d.yaxis.panecolor': (0.9, 0.9, 0.9, 0.5),
        # 'axes3d.zaxis.panecolor': (0.925, 0.925, 0.925, 0.5),
        # 'boxplot.boxprops.color': 'black',
        # 'boxplot.capprops.color': 'black',
        # 'boxplot.flierprops.color': 'black',
        # 'boxplot.flierprops.markeredgecolor': 'black',
        # 'boxplot.flierprops.markeredgewidth': 1.0,
        # 'boxplot.flierprops.markerfacecolor': 'none',
        # 'boxplot.meanprops.color': 'C2',
        # 'boxplot.meanprops.markeredgecolor': 'C2',
        # 'boxplot.meanprops.markerfacecolor': 'C2',
        # 'boxplot.meanprops.markersize': 6.0,
        # 'boxplot.medianprops.color': 'C1',
        # 'boxplot.whiskerprops.color': 'black',
        'figure.edgecolor': PLT_BG_COLOR,
        'figure.facecolor': PLT_BG_COLOR,
        # 'figure.figsize': [6.4, 4.8],
        # 'figure.frameon': True,
        # 'figure.labelsize': 'large',
        'grid.color': PLT_GRID_COLOR,
        # 'hatch.color': 'black',
        'legend.edgecolor': LEGEND_BORDER_COLOR,
        'legend.facecolor': LEGEND_BG_COLOR,
        'xtick.color': PLT_FG_COLOR,
        'ytick.color': PLT_FG_COLOR,
        'xtick.labelcolor': PLT_FG_COLOR,
        'ytick.labelcolor': PLT_FG_COLOR,
        # 'lines.color': 'C0',
        'text.color': PLT_FG_COLOR,
    }

    for k, v in color_rcParams.items():
        plt.rcParams[k] = v

    # override single char codes
    # TODO: use color name with variant from palette instead of order
    mpl.colors.get_named_colors_mapping()["b"] = COLORS[0]
    mpl.colors.get_named_colors_mapping()["o"] = COLORS[1]
    mpl.colors.get_named_colors_mapping()["g"] = COLORS[2]
    mpl.colors.get_named_colors_mapping()["r"] = COLORS[3]
    mpl.colors.get_named_colors_mapping()["m"] = COLORS[4]
    mpl.colors.get_named_colors_mapping()["y"] = COLORS[5]
    mpl.colors.get_named_colors_mapping()["c"] = COLORS[6]
    mpl.colors.get_named_colors_mapping()["k"] = P["fg0"]
    mpl.colors.get_named_colors_mapping()["w"] = P["bg0"]
    mpl.colors.get_named_colors_mapping()["black"] = P["fg0"]
    for color in color_order:
        mpl.colors.get_named_colors_mapping()[color] = P[f"fg-{color}"]




