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 {
        "stops": {
            "Pier": (0, 1),
            "Market": (2, 1),
            "Depot": (3, 3),
            "Park": (4, 1),
            "Hill": (5, 3),
        },
        "trips": [
            ("Pier", "Market", 32),
            ("Market", "Park", 32),
            ("Park", "Hill", 32),
            ("Pier", "Depot", 3),
            ("Depot", "Hill", 4),
            ("Depot", "Park", 2),
            ("Market", "Hill", 3),
            ("Market", "Depot", 4),
            ("Pier", "Park", 2),
        ],
    }


def value_table(data):
    return {
        "columns": ["From", "To", "Daily shuttle trips"],
        "rows": [list(row) for row in data["trips"]],
    }


def route_map(data, positions, frequent):
    fig = go.Figure()
    for start, end, trips in data["trips"]:
        if frequent and trips < 30:
            continue
        x, y = zip(positions[start], positions[end], strict=True)
        fig.add_scatter(
            x=x,
            y=y,
            mode="lines",
            line=dict(
                width=4 if frequent else 1, color=WARM_EDITORIAL_PALETTE[0] if frequent else MUTED
            ),
        )
    names = [name for name in positions if not frequent or name != "Depot"]
    fig.add_scatter(
        x=[positions[n][0] for n in names],
        y=[positions[n][1] for n in names],
        mode="markers+text",
        text=names,
        textposition=[
            "top left" if frequent and name == "Park" else "top center" for name in names
        ],
        textfont_size=16 if frequent else 10,
        marker=dict(color=INK, size=10),
    )
    fig.update_layout(
        template="warm_editorial",
        showlegend=False,
        title_text="Frequent shuttles follow the waterfront before turning uphill"
        if frequent
        else "Shuttle connections",
        xaxis=dict(visible=False, range=[-0.8, 5.8]),
        yaxis=dict(visible=False, range=[0, 4], scaleanchor="x"),
        margin=dict(l=25, r=25, t=65, b=55),
    )
    if frequent:
        fig.add_annotation(
            text="32 trips per day on each segment shown · North is up",
            x=0,
            y=-0.08,
            xref="paper",
            yref="paper",
            xanchor="left",
            showarrow=False,
        )
    return fig


def make_before(data):
    return route_map(
        data,
        {"Pier": (0, 1), "Hill": (2, 3), "Park": (4, 1), "Market": (5, 3), "Depot": (2, 0.6)},
        False,
    )


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