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 [
        (0, 3.8),
        (2, 3.7),
        (4, 3.6),
        (6, 3.4),
        (8, 3.2),
        (10, 2.8),
        (12, 3.3),
        (14, 3.5),
        (16, 3.6),
    ]


def make_after(data):
    stations, crest = zip(*data, strict=True)
    colors = [WARM_EDITORIAL_PALETTE[1] if h < 3 else "#B9B0A6" for h in crest]
    fig = go.Figure(go.Bar(x=stations, y=crest, marker_color=colors, width=1.6))
    for label, level, dash in [("Observed", 2.6, "dot"), ("Forecast", 3.0, "dash")]:
        fig.add_hline(y=level, line_color=INK, line_dash=dash, line_width=1)
        fig.add_annotation(
            x=16.95,
            y=level,
            text=f"{label} {level:.1f} m",
            xanchor="left",
            yshift=6 if label == "Forecast" else -7,
            showarrow=False,
        )
    fig.add_annotation(x=10, y=2.8, text="2.8 m crest", ax=0, ay=-56)
    fig.update_layout(
        template="warm_editorial",
        title_text="Forecast water clears the crest at 10 m",
        xaxis=dict(title="Distance along wall (m)", range=[-1, 21]),
        yaxis=dict(
            title="Height (m)", range=[0, 5.2], scaleanchor="x", scaleratio=1, constrain="domain"
        ),
        height=380,
        showlegend=False,
    )
    return fig


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