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_before(data):
    totals = [sum(row) for row in data]
    fig = make_subplots(
        rows=1,
        cols=2,
        specs=[[{"type": "geo"}, {"type": "xy"}]],
        subplot_titles=("Outbound volume", "Route counts"),
    )
    trace = shipment_map([int(v >= 100) for v in totals], "Class")
    trace.update(showscale=False)
    fig.add_trace(trace, row=1, col=1)
    fig.add_heatmap(
        x=list("ABCD"),
        y=list("ABCD"),
        z=list(zip(*data)),
        texttemplate="%{z}",
        colorscale=[[0, "#FAF1E5"], [1, "#70AE9D"]],
        showscale=False,
        row=1,
        col=2,
    )
    nordic_view(fig, "mercator", True)
    fig.update_layout(title_text="Nordic parcel flows", margin={"t": 85, "l": 35, "r": 15, "b": 35})
    return fig


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