import plotly.graph_objects as go


import plotly.io as pio


from plotly.subplots import make_subplots


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


MONTHS = ["Jan", "Feb", "Mar", "Apr"]


def load_data():
    return {"North": [4, 6, 8, 14], "South": [2, 3, 5, 6]}


def make_after(data):
    fig = make_subplots(
        rows=1, cols=2, subplot_titles=("A. North depot", "B. South depot"), shared_yaxes=True
    )
    for panel, (depot, values) in enumerate(data.items(), 1):
        fig.add_scatter(
            x=MONTHS,
            y=values,
            mode="lines+markers",
            name=depot,
            line_color=MUTED,
            marker={
                "size": 9,
                "color": [WARM_EDITORIAL_PALETTE[1] if v > 10 else MUTED for v in values],
            },
            row=1,
            col=panel,
        )
        fig.add_hline(y=10, line_dash="dot", line_color=MUTED, row=1, col=panel)
    fig.update_yaxes(range=[0, 16])
    fig.update_yaxes(title_text="Change (%)", row=1, col=1)
    fig.update_layout(
        template="warm_editorial",
        showlegend=False,
        title_text="Only North passed the 10% alert threshold",
        margin={"l": 60, "r": 20, "t": 85, "b": 75},
    )
    fig.add_annotation(
        x=0,
        y=-0.20,
        xref="paper",
        yref="paper",
        xanchor="left",
        showarrow=False,
        text="Change in mean delay from the same month last year",
    )
    return fig


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