make_nps_transects <- function(x) {
  suppressWarnings(
    beaufort_weather <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        ),
        Beaufort = Beaufort,
        Weather = Weather
      ) %>%
      group_by(master_key) %>%
      summarize(
        Beaufort = max(Modes(as.numeric(Beaufort)), na.rm = TRUE),
        Weather = max(Modes(Weather), na.rm = TRUE),
        .groups = "drop"
      )
  )

  if (x$optional_fields %>%
    filter(field == "Vessel") %>%
    pull(enabled) == TRUE) {
    vessel <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        ),
        Vessel = Vessel
      ) %>%
      group_by(master_key) %>%
      summarize(
        Vessel = max(Modes(Vessel), na.rm = TRUE),
        .groups = "drop"
      )
  } else {
    vessel <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        )
      ) %>%
      group_by(master_key) %>%
      summarize(
        Vessel = NA,
        .groups = "drop"
      )
  }
  if (x$optional_fields %>%
    filter(field == "CloudCover") %>%
    pull(enabled) == TRUE) {
    cloud_cover <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        ),
        CloudCover = CloudCover
      ) %>%
      group_by(master_key) %>%
      summarize(
        CloudCover = max(Modes(as.numeric(CloudCover)), na.rm = TRUE),
        .groups = "drop"
      )
  } else {
    cloud_cover <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        )
      ) %>%
      group_by(master_key) %>%
      summarize(
        CloudCover = NA,
        .groups = "drop"
      )
  }
  if (x$optional_fields %>%
    filter(field == "FogConc") %>%
    pull(enabled) == TRUE) {
    fog_conc <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        ),
        FogConc = FogConc
      ) %>%
      group_by(master_key) %>%
      summarize(
        FogConc = max(Modes(as.numeric(FogConc)), na.rm = TRUE),
        .groups = "drop"
      )
  } else {
    fog_conc <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        )
      ) %>%
      group_by(master_key) %>%
      summarize(
        FogConc = NA,
        .groups = "drop"
      )
  }
  if (x$optional_fields %>%
    filter(field == "IceType") %>%
    pull(enabled) == TRUE) {
    ice_type <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        ),
        IceType = IceType
      ) %>%
      group_by(master_key) %>%
      summarize(
        IceType = max(Modes(IceType), na.rm = TRUE),
        .groups = "drop"
      )
  } else {
    ice_type <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        )
      ) %>%
      group_by(master_key) %>%
      summarize(
        IceType = NA,
        .groups = "drop"
      )
  }
  if (x$optional_fields %>%
    filter(field == "IceConc") %>%
    pull(enabled) == TRUE) {
    ice_conc <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        ),
        IceConc = IceConc
      ) %>%
      group_by(master_key) %>%
      summarize(
        IceConc = max(Modes(as.numeric(IceConc)), na.rm = TRUE),
        .groups = "drop"
      )
  } else {
    ice_conc <- x$all_points_data %>%
      transmute(
        master_key = glue(
          "{TripID}_",
          "{str_pad(Transect, 4, pad = '0')}_",
          "{year(timestamp)}",
          "{sprintf('%02d', month(timestamp))}",
          "{sprintf('%02d', day(timestamp))}_",
          "{sprintf('%02d', split_id)}"
        )
      ) %>%
      group_by(master_key) %>%
      summarize(
        IceConc = NA,
        .groups = "drop"
      )
  }

  odata <- vessel %>%
    inner_join(beaufort_weather, by = "master_key") %>%
    inner_join(cloud_cover, by = "master_key") %>%
    inner_join(fog_conc, by = "master_key") %>%
    inner_join(ice_type, by = "master_key") %>%
    inner_join(ice_conc, by = "master_key")

  transect_tbl <- x$transects %>%
    mutate(Date = date(start_time)) %>%
    inner_join(x$nps_split, by = c("TripID", "Transect", "Date")) %>%
    inner_join(x$nps_track_centroids,
      by = c("TripID", "Transect", "Date", "split_id")
    ) %>%
    mutate(master_key = glue(
      "{TripID}_",
      "{str_pad(Transect, 4, pad = '0')}_",
      "{year(start_time.y)}",
      "{sprintf('%02d', month(start_time.y))}",
      "{sprintf('%02d', day(start_time.y))}_",
      "{sprintf('%02d', split_id)}"
    )) %>%
    select(
      master_key, TripID, Transect, start_time.y, end_time.y,
      TxWidth, length_km.y, port_obs, starboard_obs, Latitude, Longitude
    ) %>%
    left_join(odata, by = "master_key") %>%
    transmute(
      master_key = master_key,
      TripID = TripID,
      Transect = Transect,
      Latitude = Latitude,
      Longitude = Longitude,
      start_time = format(start_time.y, "%Y-%m-%d %H:%M:%S"),
      end_time = format(end_time.y, "%Y-%m-%d %H:%M:%S"),
      transect_width_m = as.numeric(gsub(
        "([0-9.]+).*",
        "\\1", TxWidth
      )),
      transect_length_km = length_km.y,
      sample_area_km2 = transect_width_m / 1000 * transect_length_km,
      Vessel = Vessel,
      Beaufort = Beaufort,
      Weather = Weather,
      CloudCover = CloudCover,
      FogConc = FogConc,
      IceType = IceType,
      IceConc = IceConc,
      PortObs = port_obs,
      StarboardObs = starboard_obs
    )
  columns_to_remove <- intersect(
    names(transect_tbl),
    x$optional_fields %>%
      filter(enabled == FALSE) %>%
      pull(field)
  )

  nps_transects <- transect_tbl %>%
    select(-any_of(columns_to_remove))

  double_column_names <- nps_transects %>%
    select(where(
      ~ is_double(.) &
        !inherits(., "Date") &
        !inherits(., "POSIXt") &
        !inherits(., "hms")
    )) %>%
    names()
  columns_to_round <- ui_column_vector(names(nps_transects), double_column_names)

  timestamp_column_names <- nps_transects %>%
    select(
      where(
        ~ is.POSIXt(.)
      )
    ) %>%
    names()
  timestamp_columns <- ui_column_vector(names(nps_transects), timestamp_column_names)

  x$nps_transects <- nps_transects
  x$nps_transects_round <- columns_to_round
  x$nps_transects_timestamp_columns <- timestamp_columns

  x
}

make_nps_obs <- function(x) {
  tdata <- x$transects %>%
    mutate(Date = date(start_time)) %>%
    inner_join(x$nps_split, by = c("TripID", "Transect", "Date")) %>%
    transmute(
      master_key = glue(
        "{TripID}_",
        "{str_pad(Transect, 4, pad = '0')}_",
        "{year(start_time.y)}",
        "{sprintf('%02d', month(start_time.y))}",
        "{sprintf('%02d', day(start_time.y))}_",
        "{sprintf('%02d', split_id)}"
      ),
      transect_width = as.numeric(gsub(
        "([0-9.]+).*",
        "\\1", TxWidth
      )),
      length_km = length_km.y,
      sample_area = transect_width / 1000 * length_km
    ) %>%
    select(-transect_width)

  nps_obs <- x$all_obs_data %>%
    mutate(master_key = glue(
      "{TripID}_",
      "{str_pad(Transect, 4, pad = '0')}_",
      "{year(timestamp)}",
      "{sprintf('%02d', month(timestamp))}",
      "{sprintf('%02d', day(timestamp))}_",
      "{sprintf('%02d', split_id)}"
    )) %>%
    inner_join(tdata, by = "master_key") %>%
    mutate(
      Count = as.numeric(Count),
      density = if_else(
        ONOFFTX == "ON",
        Count / sample_area,
        as.numeric(NA)
      ),
      timestamp = format(timestamp, "%Y-%m-%d %H:%M:%S")
    ) %>%
    select(c(
      master_key, timestamp, TripID, Transect, Latitude, Longitude,
      Species, Count, PrimaryBehavior, sample_area, density, Comments,
      x$optional_fields %>%
        filter(enabled == TRUE) %>%
        pull(field)
    ))

  double_column_names <- nps_obs %>%
    select(where(
      ~ is_double(.) &
        !inherits(., "Date") &
        !inherits(., "POSIXt") &
        !inherits(., "hms")
    )) %>%
    names()
  columns_to_round <- ui_column_vector(names(nps_obs), double_column_names)

  x$nps_obs <- nps_obs
  x$nps_obs_round <- columns_to_round

  x
}

# vim: set ft=r fenc=utf-8 tw=80 ts=2 sw=2 sts=2:
