## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(dragmapr)

make_square <- function(x0, y0, size = 100000) {
  sf::st_polygon(list(rbind(
    c(x0, y0), c(x0 + size, y0), c(x0 + size, y0 + size),
    c(x0, y0 + size), c(x0, y0)
  )))
}

regions <- sf::st_sf(
  region = c("North", "South", "East", "West"),
  label  = c("North", "South", "East", "West"),
  geometry = sf::st_sfc(
    make_square(0,       140000),
    make_square(0,            0),
    make_square( 140000,  70000),
    make_square(-140000,  70000),
    crs = 3857
  )
)

## ----eval = FALSE-------------------------------------------------------------
# # Minimal call — one grouping column used for both groups and labels
# drag_map_prototype(regions, region_col = "region", open = interactive())
# 
# # Separate column for label text
# drag_map_prototype(regions, region_col = "region", label_col = "label", open = interactive())

## ----eval = FALSE-------------------------------------------------------------
# # No labels
# drag_map_prototype(regions, region_col = "region", labels = FALSE, open = interactive())
# 
# # Labels follow regions but cannot be dragged separately
# drag_map_prototype(regions, region_col = "region",
#                    draggable_labels = FALSE, open = interactive())
# 
# # Custom label data frame
# custom_labels <- make_region_labels(regions, region_col = "region", label_col = "label")
# drag_map_prototype(regions, region_col = "region",
#                    labels = custom_labels, open = interactive())

## ----eval = FALSE-------------------------------------------------------------
# # Rounded-box markers (default) — adjust size and text
# drag_map_prototype(
#   regions, region_col = "region",
#   label_marker_shape = "rect",
#   label_text_size    = 12,
#   label_width        = 80,
#   label_height       = 32,
#   open = interactive()
# )
# 
# # Circle markers
# drag_map_prototype(
#   regions, region_col = "region",
#   label_marker_shape = "circle",
#   label_radius       = 18,
#   label_text_size    = 11,
#   open = interactive()
# )
# 
# # Text only — no visible marker, just draggable text
# drag_map_prototype(
#   regions, region_col = "region",
#   label_marker_shape = "none",
#   label_text_size    = 13,
#   open = interactive()
# )
# 
# # Annotation boxes (info-box style with connectors)
# notes <- as_drag_annotations(
#   make_region_labels(regions, region_col = "region"),
#   width_px       = 160,
#   height_px      = 70,
#   connector      = TRUE,
#   connector_type = "elbow"
# )
# drag_map_prototype(
#   regions, region_col = "region",
#   labels           = notes,
#   label_box_width  = 160,
#   label_box_height = 70,
#   open = interactive()
# )

## ----eval = FALSE-------------------------------------------------------------
# drag_map_prototype(
#   regions, region_col = "region",
#   labels              = notes,   # labels with connector = TRUE
#   connector_color     = "#334155",
#   connector_linewidth = 2,
#   connector_linetype  = "dashed",
#   connector_endpoint  = "arrow",
#   connector_smart     = TRUE,
#   open = interactive()
# )

## ----eval = FALSE-------------------------------------------------------------
# drag_map_prototype(
#   regions, region_col = "region",
#   show_origin_outlines = TRUE,
#   show_movement_connectors = TRUE,
#   movement_connector_color = "#64748b",
#   movement_connector_linetype = "dashed",
#   movement_connector_endpoint = "open",
#   show_drag_trail = TRUE,
#   open = interactive()
# )

## ----eval = FALSE-------------------------------------------------------------
# render_dragged_map(
#   regions,
#   region_offsets = region_offsets,
#   region_col = "region",
#   show_origin_outlines = TRUE,
#   show_movement_connectors = TRUE,
#   movement_connector_linetype = "dotted",
#   movement_connector_endpoint = "closed"
# )

## ----eval = FALSE-------------------------------------------------------------
# # Resume from previously downloaded CSVs
# region_csv <- read_offsets("drag_region_offsets.csv")
# label_csv  <- read_label_state("drag_label_offsets.csv")
# 
# drag_map_prototype(
#   regions, region_col = "region",
#   region_offsets = region_csv,
#   label_offsets  = label_csv,
#   open = interactive()
# )

## ----eval = FALSE-------------------------------------------------------------
# render_dragmapr_project(
#   "dragmapr-project.zip",
#   file = "dragged-map.png"
# )

## ----eval = FALSE-------------------------------------------------------------
# my_palette <- c(
#   North = "#4C78A8",
#   South = "#F58518",
#   East  = "#54A24B",
#   West  = "#B279A2"
# )
# 
# drag_map_prototype(
#   regions, region_col = "region",
#   region_palette = my_palette,
#   show_legend    = TRUE,
#   open = interactive()
# )

## ----eval = FALSE-------------------------------------------------------------
# drag_map_prototype(
#   regions, region_col = "region",
#   region_palette  = my_palette,
#   show_legend     = TRUE,
#   legend_position = "bottom",   # "top" | "left" | "right" | "none"
#   legend_title    = "Region",
#   legend_values   = c("North", "East"),
#   max_legend_keys = 25,
#   open = interactive()
# )

## ----eval = FALSE-------------------------------------------------------------
# drag_map_prototype(
#   regions, region_col = "region",
#   label_values = c("North", "East"),
#   open = interactive()
# )

## ----eval = FALSE-------------------------------------------------------------
# # Write to a chosen path without opening (e.g. CI, server, or Shiny)
# drag_map_prototype(
#   regions, region_col = "region",
#   file       = "output/my_map.html",
#   map_background = "light_grid",
#   side_panel = TRUE,
#   open       = FALSE
# )
# 
# # Embedded in a Shiny app — hide the panel; the app provides its own controls
# drag_map_prototype(
#   regions, region_col = "region",
#   side_panel = FALSE,
#   file       = tempfile(fileext = ".html")
# )

## -----------------------------------------------------------------------------
region_offsets <- data.frame(
  region = c("North", "South", "East", "West"),
  dx_m   = c(0, 0,  60000, -60000),
  dy_m   = c(50000, -50000, 0, 0)
)

label_state <- data.frame(
  label_id = c("North", "South", "East", "West"),
  region   = c("North", "South", "East", "West"),
  dx_m     = c(0, 0,  25000, -25000),
  dy_m     = c(30000, -30000, 0, 0)
)

render_dragged_map(
  regions,
  region_offsets = region_offsets,
  region_col     = "region",
  label_col      = "label",
  label_offsets  = label_state,
  title          = "Synthetic draggable map"
)

## -----------------------------------------------------------------------------
notes <- as_drag_annotations(data.frame(
  label_id = "east-note",
  region   = "East",
  label    = "The East label can become a longer draggable note.",
  x = 210000, y = 120000
), connector = TRUE, connector_type = "squiggle")

render_dragged_map(
  regions,
  region_offsets      = region_offsets,
  region_col          = "region",
  labels              = notes,
  label_offsets       = data.frame(
    label_id = "east-note", region = "East",
    dx_m = 80000, dy_m = 45000
  ),
  connector_linewidth = 0.8,
  connector_linetype  = "dotted",
  connector_endpoint  = "arrow",
  legend_title        = "Region",
  map_background      = "light_grid",
  title               = "Static output with an annotation box"
)

## ----eval = FALSE-------------------------------------------------------------
# source(system.file("examples", "smoke_examples.R", package = "dragmapr"))

## ----eval = FALSE-------------------------------------------------------------
# if (interactive()) {
#   shiny::runApp(system.file("examples", "shiny_draggable_plot.R",   package = "dragmapr"))
#   shiny::runApp(system.file("examples", "shiny_custom_labels.R",    package = "dragmapr"))
#   shiny::runApp(system.file("examples", "shiny_draggable_export.R", package = "dragmapr"))
#   shiny::runApp(system.file("examples", "shiny_spatial_studio.R",   package = "dragmapr"))
#   shiny::runApp(system.file("examples", "shiny_static_export.R",    package = "dragmapr"))
# }

