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"


def load_data():
    return [
        (2020, 3600, 150),
        (2021, 3960, 165),
        (2022, 4500, 180),
        (2023, 5200, 200),
        (2024, 5400, 200),
        (2025, 6160, 220),
    ]


def make_after(data):
    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,
            },
        )
    )
    years, miles, gallons = zip(*data, strict=True)
    mpg = [m / g for m, g in zip(miles, gallons, strict=True)]
    fig = go.Figure(
        go.Scatter(
            x=years,
            y=mpg,
            mode="lines+markers",
            line_color=MUTED,
            marker=dict(size=[7] * 5 + [12], color=[MUTED] * 5 + [WARM_EDITORIAL_PALETTE[0]]),
        )
    )
    fig.add_table(
        domain=dict(x=[0.64, 1], y=[0.02, 0.97]),
        header=dict(values=["Year", "Miles", "Gal.", "MPG"], fill_color=RULE, height=27),
        cells=dict(
            values=[years, miles, gallons, mpg],
            format=[None, ",d", "d", ".0f"],
            fill_color=CANVAS,
            height=25,
        ),
    )
    fig.update_layout(
        template="warm_editorial",
        width=720,
        height=390,
        title="Fleet fuel economy reaches 28 mpg",
        xaxis=dict(domain=[0, 0.51], title="Year", dtick=2, tickformat="d"),
        yaxis_title="Miles per gallon",
        yaxis_range=[23, 29],
        margin=dict(l=65, r=12, t=70, b=108),
        showlegend=False,
    )
    for i, anchor in [(0, "left"), (-1, "right")]:
        fig.add_annotation(
            x=years[i],
            y=mpg[i],
            text=f"{years[i]}: {mpg[i]:.0f} mpg",
            showarrow=False,
            xanchor=anchor,
            yshift=16,
        )
    fig.add_annotation(
        text="Source: illustrative fleet fuel logs",
        xref="paper",
        yref="paper",
        x=0,
        y=-0.40,
        xanchor="left",
        showarrow=False,
    )
    return fig


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