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 [
        (2019, 385, 98),
        (2020, 400, 100),
        (2021, 416, 103),
        (2022, 440, 108),
        (2023, 460, 112),
        (2024, 495, 116),
        (2025, 522, 120),
    ]


def make_after(data):
    years = [year for year, _, _ in data]
    spending = [amount * 100 / index for _, amount, index in data]
    fig = go.Figure(
        go.Scatter(
            x=years, y=spending, mode="lines+markers", line={"color": WARM_EDITORIAL_PALETTE[0]}
        )
    )
    fig.update_layout(
        template="warm_editorial",
        title="Library spending rose after inflation",
        width=680,
        height=380,
        xaxis_title="Fiscal year",
        xaxis_tickvals=years,
        yaxis_title="Thousands of constant 2020 dollars",
        yaxis_range=[390, 440],
    )
    return fig


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