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_before(data):
    totals = [sum(row) for row in data]
    fig = go.Figure(shipment_map(totals, "Parcels"))
    labels = [" / ".join(str(v) for v in row) for row in data]
    fig.add_scattergeo(
        lon=[7, 15, 9, 26],
        lat=[62, 61, 56, 64],
        text=labels,
        mode="text",
        textfont={"size": 12, "color": [INK, CANVAS, INK, INK]},
    )
    nordic_view(fig, "equal earth", False)
    fig.update_layout(
        title_text="Outbound parcels by office and destination",
        margin={"t": 55, "b": 10, "l": 10, "r": 30},
    )
    return fig


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