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 [
        ("Broth A", [10, 20, 40, 80, 160]),
        ("Gel A", [10, 14, 20, 28, 40]),
        ("Broth B", [12, 24, 48, 96, 192]),
        ("Gel B", [12, 17, 24, 34, 48]),
    ]


def make_after(data):
    from plotly.subplots import make_subplots

    fig = make_subplots(rows=1, cols=2, shared_yaxes=True, subplot_titles=("Broth", "Gel"))
    for name, counts in data:
        medium, culture = name.split()
        col = 1 if medium == "Broth" else 2
        fig.add_trace(
            go.Scatter(
                x=list(range(5)),
                y=counts,
                mode="lines+markers+text",
                text=[""] * (len(counts) - 1) + [f"{culture} · {counts[-1]}"],
                textposition="middle right",
                line_color=WARM_EDITORIAL_PALETTE[0 if culture == "A" else 1],
                name=culture,
                showlegend=False,
            ),
            row=1,
            col=col,
        )
    fig.update_xaxes(title_text="Hours since inoculation", range=[-0.1, 5.3], dtick=1)
    fig.update_yaxes(type="log", tickvals=[10, 20, 40, 80, 160], range=[0.94, 2.4])
    fig.update_yaxes(title_text="Count (CFU/mL)", row=1, col=1)
    fig.update_layout(
        template="warm_editorial", title_text="Broth counts double every hour", margin=dict(t=85)
    )
    return fig


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