## ----include = FALSE----------------------------------------------------------
startTime <- Sys.time()
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  crop = NULL
)

library(dnaEPICO)

## ----overview-helpers, echo = FALSE, results = "asis"-------------------------
cat("
<style>
.overview-figure {
  margin: 1.5rem 0 2rem;
}

.overview-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 0.5rem;
}

.overview-zoom-button {
  background: #ffffff;
  border: 1px solid #2c7fb8;
  border-radius: 6px;
  color: #2c7fb8;
  cursor: pointer;
  font-size: 0.875rem;
  line-height: 1.2;
  padding: 5px 9px;
}

.overview-zoom-button:hover,
.overview-zoom-button:focus {
  background: #edf6fb;
}

.overview-zoom-value {
  align-items: center;
  color: #4b5563;
  display: inline-flex;
  font-size: 0.875rem;
  min-width: 44px;
}

.overview-svg-frame {
  background: #ffffff;
  border: 1px solid #d9e2ec;
  border-radius: 6px;
  max-height: 680px;
  overflow: auto;
  padding: 10px;
}

.overview-svg-frame img {
  display: block;
  height: auto;
  max-width: none;
  width: calc(1600px * var(--overview-scale, 1));
}

.overview-figure:fullscreen {
  background: #ffffff;
  margin: 0;
  overflow: hidden;
  padding: 16px;
}

.overview-figure:fullscreen .overview-svg-frame {
  height: calc(100vh - 88px);
  max-height: none;
}

@media (max-width: 768px) {
  .overview-svg-frame {
    max-height: 520px;
  }

  .overview-svg-frame img {
    width: calc(1200px * var(--overview-scale, 1));
  }
}
</style>
<script>
function setOverviewZoom(figureId, scale) {
  var figure = document.getElementById(figureId);
  if (!figure) {
    return;
  }

  var next = Math.max(0.5, Math.min(3, scale));
  figure.dataset.scale = next.toFixed(2);
  figure.style.setProperty('--overview-scale', next.toString());

  var label = figure.querySelector('.overview-zoom-value');
  if (label) {
    label.textContent = Math.round(next * 100) + '%';
  }
}

function changeOverviewZoom(figureId, delta) {
  var figure = document.getElementById(figureId);
  var current = figure ? parseFloat(figure.dataset.scale || '1') : 1;
  setOverviewZoom(figureId, current + delta);
}

function openOverviewFullscreen(figureId) {
  var figure = document.getElementById(figureId);
  if (!figure || !figure.requestFullscreen) {
    return;
  }

  figure.requestFullscreen();
}
</script>
")

overview_svgs <- c(
  preprocessingMinfiEwasWater =
    "preprocessingMinfiEwasWater_pipeline_overview.svg",
  svaEnmix =
    "svaEnmix_pipeline_overview.svg",
  preprocessingPheno =
    "preprocessingPheno_pipeline_overview.svg",
  methylationGLM =
    "methylationGLM_pipeline_overview.svg",
  methylationLME =
    "methylationLME_pipeline_overview.svg",
  dnamReport =
    "dnamReport_pipeline_overview.svg"
)

html_escape <- function(x) {
  x <- gsub("&", "&amp;", x, fixed = TRUE)
  x <- gsub("<", "&lt;", x, fixed = TRUE)
  x <- gsub(">", "&gt;", x, fixed = TRUE)
  x <- gsub('"', "&quot;", x, fixed = TRUE)
  x
}

svg_to_html <- function(svg_path, title = NULL) {
  if (!file.exists(svg_path)) {
    stop("Missing overview SVG: ", svg_path)
  }

  if (is.null(title)) {
    title <- sub(
      "_pipeline_overview$",
      "() overview",
      tools::file_path_sans_ext(basename(svg_path))
    )
  }

  figure_id <- gsub("[^A-Za-z0-9_-]", "-", tools::file_path_sans_ext(
    basename(svg_path)
  ))

  cat(sprintf(
    '<div class="overview-figure" id="%s" data-scale="1">\n',
    figure_id
  ))
  cat('<div class="overview-controls">\n')
  cat(sprintf(
    '<button class="overview-zoom-button" type="button" onclick="changeOverviewZoom(\'%s\', 0.25)">Zoom +</button>\n',
    figure_id
  ))
  cat(sprintf(
    '<button class="overview-zoom-button" type="button" onclick="changeOverviewZoom(\'%s\', -0.25)">Zoom -</button>\n',
    figure_id
  ))
  cat(sprintf(
    '<button class="overview-zoom-button" type="button" onclick="setOverviewZoom(\'%s\', 1)">Reset</button>\n',
    figure_id
  ))
  cat(sprintf(
    '<button class="overview-zoom-button" type="button" onclick="openOverviewFullscreen(\'%s\')">Full screen</button>\n',
    figure_id
  ))
  cat('<span class="overview-zoom-value">100%</span>\n')
  cat('</div>\n')
  cat('<div class="overview-svg-frame">\n')
  cat(sprintf(
    '<img src="%s" alt="%s">\n',
    html_escape(svg_path),
    html_escape(title)
  ))
  cat('\n</div>\n</div>\n')
}

## ----preprocessingMinfiEwasWater-overview, echo = FALSE, results = "asis"-----
svg_to_html(
  overview_svgs[["preprocessingMinfiEwasWater"]]
)

## ----svaEnmix-overview, echo = FALSE, results = "asis"------------------------
svg_to_html(
  overview_svgs[["svaEnmix"]])

## ----preprocessingPheno-overview, echo = FALSE, results = "asis"--------------
svg_to_html(
  overview_svgs[["preprocessingPheno"]])

## ----methylationGLM-overview, echo = FALSE, results = "asis"------------------
svg_to_html(
  overview_svgs[["methylationGLM"]])

## ----methylationLME-overview, echo = FALSE, results = "asis"------------------
svg_to_html(
  overview_svgs[["methylationLME"]])

## ----dnamReport-overview, echo = FALSE, results = "asis"----------------------
svg_to_html(
  overview_svgs[["dnamReport"]])

## ----echo = FALSE-------------------------------------------------------------
Sys.time()

## ----echo = FALSE-------------------------------------------------------------
totalTime <- diff(c(startTime, Sys.time()))
round(totalTime, digits = 3)

## ----echo = FALSE-------------------------------------------------------------
sessionInfo()

