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"]


CLERKS = [8, 20, 5, 6]


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", "Clerks", *COUNTRIES],
        "rows": [
            [country, staff, *row]
            for country, staff, row in zip(COUNTRIES, CLERKS, 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):
    rates = [sum(row) / count for row, count in zip(data, CLERKS, strict=True)]
    fig = make_subplots(
        rows=2,
        cols=2,
        specs=[[{"type": "geo"}, {}], [{"colspan": 2}, None]],
        vertical_spacing=0.2,
        horizontal_spacing=0.16,
        subplot_titles=(
            "Nordic dispatch offices",
            "Parcels per clerk",
            "Parcels by origin and destination",
        ),
    )
    trace = shipment_map(rates, "Parcels per clerk")
    trace.update(showscale=False, colorscale=[[0, "#E4EFEA"], [1, "#70AE9D"]])
    fig.add_trace(trace, row=1, col=1)
    fig.add_scattergeo(
        lon=[7, 15, 9, 26],
        lat=[62, 61, 56, 64],
        text=COUNTRIES,
        mode="text",
        textfont={"size": 13, "color": INK},
        row=1,
        col=1,
    )
    nordic_view(fig, "equal earth", True)
    fig.add_heatmap(
        x=COUNTRIES,
        y=COUNTRIES,
        z=data,
        texttemplate="%{z}",
        colorscale=[[0, "#FAF1E5"], [1, "#70AE9D"]],
        showscale=False,
        row=2,
        col=1,
    )
    fig.add_bar(
        x=rates,
        y=COUNTRIES,
        orientation="h",
        marker_color=WARM_EDITORIAL_PALETTE[0],
        text=rates,
        textposition="inside",
        row=1,
        col=2,
    )
    fig.update_xaxes(title_text="Destination", row=2, col=1)
    fig.update_yaxes(autorange="reversed")
    fig.update_layout(
        template="warm_editorial",
        title_text="Denmark dispatches 12 parcels per clerk",
        height=600,
        showlegend=False,
        margin={"l": 75, "r": 20, "b": 65, "t": 85},
    )
    return fig


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