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


COUNTRIES = ["Norway", "Sweden", "Denmark", "Finland"]


ISO = ["NOR", "SWE", "DNK", "FIN"]


def load_data():
    return [
        [0, 40, 30, 10],
        [60, 0, 50, 30],
        [10, 30, 0, 20],
        [20, 10, 30, 0],
    ]


def value_table(data):
    return {
        "columns": ["Dispatch office", *COUNTRIES],
        "rows": [[country, *row] for country, row in zip(COUNTRIES, data, strict=True)],
    }


def shipment_map(values, title):
    return go.Choropleth(
        locations=ISO,
        z=values,
        text=COUNTRIES,
        colorscale=[[0, "#E4EFEA"], [1, WARM_EDITORIAL_PALETTE[0]]],
        colorbar={"title": title, "len": 0.72},
    )


def nordic_view(fig, projection, context):
    fig.update_geos(
        scope="europe",
        projection_type=projection,
        lonaxis_range=[3, 34],
        lataxis_range=[54, 72],
        showland=context,
        landcolor="#E7DDD2",
        showcountries=context,
        showcoastlines=context,
        bgcolor=CANVAS,
    )


def make_after(data):
    totals = [sum(row) for row in data]
    fig = make_subplots(
        rows=1,
        cols=2,
        specs=[[{"type": "geo"}, {"type": "xy"}]],
        subplot_titles=("Outbound parcels per shift", "Origin rows · destination columns"),
    )
    classes = [int(total >= 80) + int(total >= 120) for total in totals]
    trace = shipment_map(classes, "Parcels")
    trace.update(
        zmin=-0.5,
        zmax=2.5,
        colorscale=[
            [0, "#D3E5DD"],
            [1 / 3, "#D3E5DD"],
            [1 / 3, "#76AB9B"],
            [2 / 3, "#76AB9B"],
            [2 / 3, "#176D60"],
            [1, "#176D60"],
        ],
        showscale=False,
    )
    fig.add_trace(trace, row=1, col=1)
    fig.add_scattergeo(
        lon=[7, 15, 9, 26],
        lat=[62, 61, 56, 64],
        textfont={"color": [INK, CANVAS, INK, INK]},
        text=ISO,
        mode="text",
        row=1,
        col=1,
    )
    fig.add_heatmap(
        x=ISO,
        y=ISO,
        z=data,
        texttemplate="%{z}",
        colorscale=[[0, "#FAF1E5"], [1, "#70AE9D"]],
        showscale=False,
        row=1,
        col=2,
    )
    fig.update_yaxes(autorange="reversed")
    nordic_view(fig, "equal earth", True)
    fig.update_layout(
        template="warm_editorial",
        title_text="Sweden sends 60 parcels to Norway and receives 40",
        showlegend=False,
        margin={"t": 85, "l": 15, "r": 15, "b": 55},
    )
    fig.add_annotation(
        text="Outbound classes, light to dark: under 80 · 80–119 · 120 or more",
        x=0,
        y=-0.12,
        xref="paper",
        yref="paper",
        xanchor="left",
        showarrow=False,
    )
    return fig


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