---
title: "Paris Metro Lines"
author: 'Albert Rapp'
format: 
    typst:
        filters: ['./filter_recording.lua']
---


```{r}
#| echo: false
library(gt)
df_metro_longer <- gt::metro |>
  dplyr::select(name, passengers, lines) |>
  dplyr::slice(1:10) |>
  tidyr::separate_longer_delim(
    cols = lines,
    delim = ', '
  )

create_line_badge <- function(line) {
  color_bg_dict <- c(
    '1' = '#FFCE00',
    '2' = '#0064B0',
    '3' = '#9F9825',
    '3bis' = '#98D4E2',
    '4' = '#C04191',
    '5' = '#F28E42',
    '6' = '#83C491',
    '7' = '#F3A4BA',
    '7bis' = '#83C491',
    '8' = '#CEADD2',
    '9' = '#D5C900',
    '10' = '#E3B32A',
    '11' = '#8D5E2A',
    '12' = '#00814F',
    '13' = '#98D4E2',
    '14' = '#662483'
  )
  lines_color_fg_white <- c('2', '3', '4', '11', '12', '14')
  color_text <- if (line %in% lines_color_fg_white) "#ffffff" else "#000000"

  width_px <- 25
  htmltools::span(
    htmltools::span(
      line,
      style = htmltools::css(margin = 'auto')
    ),
    'typst:fill' = glue::glue('rgb("{color_bg_dict[line]}")'),
    'typst:text:fill' = glue::glue('rgb("{color_text}")'),
    style = htmltools::css(
      display = 'inline-flex',
      width = paste0(width_px, 'px'),
      height = paste0(width_px, 'px'),
      background = color_bg_dict[line],
      color = color_text,
      border_radius = '100%'
    ),
    class = 'my-box'
  ) |>
    as.character()
}

df_metro_longer |>
  dplyr::mutate(lines = purrr::map_chr(lines, create_line_badge)) |>
  dplyr::summarise(
    lines = paste0(lines, collapse = ' '),
    .by = c(name, passengers)
  ) |>
  gt() |>
  fmt_number(columns = passengers, decimals = 0) |>
  fmt_markdown(columns = lines) |>
  data_color(
    columns = passengers,
    method = "numeric",
    palette = c('white', "#0064B0"),
    domain = c(0, max(df_metro_longer$passengers))
  )
```






