"""
Situation map (qualitative symbols)
===================================

Located qualitative points, differentiated by category, with a zoom
window on the dense Paris region. Data: hospital helipads in mainland
France, distinguished by night-flight capability (data.gouv.fr, CC-BY).

Hover a marker to read its name.
"""

from pathlib import Path

import geopandas as gpd
import pandas as pd

from mappyng import Map, BasemapLayer, SituationLayer, VectorLayer
from mappyng.data import load_france_departments, load_europe

europe = load_europe().to_crs(2154)
deps = load_france_departments().to_crs(2154)
df = pd.read_csv(Path("data") / "helistations.csv")
points = gpd.GeoDataFrame(
    df,
    geometry=gpd.points_from_xy(df["longitude"], df["latitude"]),
    crs="EPSG:4326",
).to_crs(2154)

bbox_metro = [90000, 6040000, 1280000, 7150000]
cartouches = {
    0: {"bbox": [-6890454, 1780151, -6799589, 1874186], "crs": 3857,
        "cartouche_title": "Guadeloupe", "cartouche_title_size": 7,
        "border_radius": 6, "box_shadow": True},
    1: {"bbox": [-6826862, 1609607, -6759494, 1685367], "crs": 3857,
        "cartouche_title": "Martinique", "cartouche_title_size": 7,
        "border_radius": 6, "box_shadow": True},
    2: {"bbox": [-6136680, 235261, -5707791, 671780], "crs": 3857,
        "cartouche_title": "French Guiana", "cartouche_title_size": 7,
        "border_radius": 6, "box_shadow": True},
    3: {"bbox": [6136383, -2448160, 6226316, -2366992], "crs": 3857,
        "cartouche_title": "Reunion", "cartouche_title_size": 7,
        "border_radius": 6, "box_shadow": True},
    4: {"bbox": [5006226, -1459183, 5050208, -1416184], "crs": 3857,
        "cartouche_title": "Mayotte", "cartouche_title_size": 7,
        "border_radius": 6, "box_shadow": True},
}

m = Map(europe, bbox=bbox_metro, width=900, height=1000, padding=25,
        facecolor="#e8eef2", border_radius=10,
        cartouche_params=cartouches, cartouche_spacing=15)
m.add(BasemapLayer())
m.add(VectorLayer(deps, stroke="#9aa6b2", stroke_width=0.2, on_cartouches=True))
m.add_shadow(europe, {"query": "code_pays_iso3=='FRA'", "on_cartouches": True})

# Zoom on the Paris region (Lambert-93), where helipads are dense.
m.add_zoom({
    "bbox": [586377, 6780533, 739396, 6904563],
    "border_radius": 8,
    "box_shadow": {"dx": 2, "dy": 2, "blur": 4, "opacity": 0.3},
})
m.add(BasemapLayer())

m.add(SituationLayer(
    points,
    column="vol_nuit",
    symbol={
        "Night flight": {"marker": "circle", "fill": "#1f78b4", "size": 6},
        "Day only": {"marker": "triangle", "fill": "#e31a1c", "size": 9},
    },
    legend={"title": "Hospital helipads"},
))

m.title("Hospital helipads", subtitle="Night-flight capability")
m.scale_bar(length=100000, label="100 km", position={"x": 0.46, "y": 0.9},
            opacity=0.8)
m.source("Source: data.gouv.fr (CC-BY)")

# Hover tooltips. The gallery embeds an interactive SVG from this dict; the
# same call also yields a standalone HTML file.
TOOLTIP = {"columns": ["nom", "vol_nuit"],
           "aliases": {"nom": "Site", "vol_nuit": "Capability"}}
interactive = m.to_interactive(**TOOLTIP)
# interactive.save("situation.html")
