import plotly.graph_objects as go


import plotly.io as pio


INK = "#1C1917"


MUTED = "#57534E"


WARM_EDITORIAL_PALETTE = [
    "#0F766E",
    "#C2410C",
    "#1D4ED8",
    "#A16207",
    "#7E22CE",
    "#475569",
    "#0369A1",
    "#1C1917",
]


CANVAS = "#FFF7ED"


RULE = "#E7DDD2"


pio.templates["warm_editorial"] = go.layout.Template(
    layout=go.Layout(
        colorway=WARM_EDITORIAL_PALETTE,
        font={"family": "Inter, system-ui, -apple-system, sans-serif", "size": 13, "color": INK},
        title={
            "x": 0,
            "xanchor": "left",
            "font": {"family": "Georgia, 'Times New Roman', serif", "size": 18, "color": INK},
        },
        paper_bgcolor=CANVAS,
        plot_bgcolor=CANVAS,
        xaxis={
            "showgrid": False,
            "showline": True,
            "linecolor": "#A8A29E",
            "tickcolor": "#A8A29E",
            "tickfont": {"color": MUTED},
            "automargin": True,
        },
        yaxis={
            "showgrid": True,
            "gridcolor": RULE,
            "gridwidth": 1,
            "showline": False,
            "tickcolor": "#A8A29E",
            "tickfont": {"color": MUTED},
            "zeroline": False,
            "automargin": True,
        },
    )
)


def load_data():
    return [
        ("Air", [19, 21, 22, 26]),
        ("Mist", [22, 25, 26, 27]),
        ("Steam", [24, 30, 34, 40]),
        ("Wrap", [26, 27, 29, 30]),
    ]


def make_before(data):
    import pandas as pd
    import plotly.express as px
    from statistics import mean, stdev

    frame = pd.DataFrame(
        [(method, mean(values), stdev(values)) for method, values in data],
        columns=["Method", "Strength", "Spread"],
    )
    fig = px.bar(
        frame,
        x="Method",
        y="Strength",
        color="Method",
        error_y="Spread",
        color_discrete_sequence=["#0072B2", "#E69F00", "#009E73", "#CC79A7"],
        template="simple_white",
    )
    for (method, values), symbol in zip(
        data, ["circle", "circle-open", "diamond", "diamond-open"], strict=True
    ):
        points = [*values, mean(values)]
        fig.add_trace(
            go.Scatter3d(
                x=[method] * len(points),
                y=[0] * len(points),
                z=points,
                mode="markers",
                marker=dict(symbol=symbol, size=[v / 2 for v in points], sizemode="diameter"),
                name=method,
                showlegend=False,
            )
        )
    fig.update_layout(
        title_text="Mean strength and specimen results",
        xaxis_domain=[0, 0.43],
        yaxis_title="Mean (MPa) ± SD",
        scene=dict(
            camera=dict(eye=dict(x=1.7, y=1.9, z=1.4)),
            domain=dict(x=[0.49, 1]),
            xaxis_title="Method",
            yaxis_visible=False,
            zaxis_title="MPa",
        ),
        showlegend=False,
    )
    return fig


if __name__ == "__main__":
    print(make_before(load_data()).to_json())
