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,
        },
    )
)


from statistics import mean, stdev


def load_data():
    return [
        [14, 16, 15, 17, 14, 18, 16, 15],
        [17, 19, 18, 20, 16, 19, 18, 21],
        [18, 20, 19, 22, 18, 20, 21, 24],
        [20, 21, 22, 23, 21, 24, 23, 27],
    ]


def make_before(data):
    fig = go.Figure()
    for day, readings in enumerate(data, 1):
        fig.add_violin(
            x=[day] * len(readings),
            y=readings,
            box_visible=True,
            points=False,
            line_color=MUTED,
            showlegend=False,
        )
    fig.add_scatter(
        x=[1, 2, 3, 4],
        y=[mean(v) for v in data],
        mode="lines",
        line_shape="spline",
        line_color=INK,
        showlegend=False,
    )
    fig.update_layout(
        template="warm_editorial",
        title="Daily output in a pilot production run",
        xaxis_title="Trial day",
        yaxis_title="Units per hour",
        xaxis_dtick=1,
    )
    return fig


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