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_before(data):
    stations, crest = zip(*data, strict=True)
    colors = [
        "#6e5773",
        "#a87b91",
        "#d18178",
        "#78758f",
        "#729d89",
        "#91a49d",
        "#8d844a",
        "#c5a669",
        "#6984a1",
    ]
    fig = go.Figure(
        go.Bar(
            x=stations,
            y=crest,
            name="Wall",
            marker_color=colors,
            marker_pattern_shape="/",
            showlegend=False,
        )
    )
    for label, level, color in [("Observed", 2.6, "#bb0000"), ("Forecast", 3.0, "#008000")]:
        fig.add_trace(
            go.Scatter(
                x=stations, y=[level] * len(stations), mode="lines", name=label, line_color=color
            )
        )
    fig.add_trace(
        go.Indicator(
            mode="gauge+number",
            value=min(crest),
            title_text="Lowest crest (m)",
            domain=dict(x=[0.78, 1], y=[0.24, 0.80]),
            gauge_axis_range=[0, 4],
        )
    )
    fig.update_layout(
        template="simple_white",
        title_text="Flood wall survey",
        xaxis=dict(domain=[0, 0.68], title="Distance along wall (m)"),
        yaxis=dict(title="Height (m)", showgrid=True, gridwidth=3, gridcolor="#555555"),
        legend=dict(orientation="h", y=-0.2),
    )
    return fig


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