render_sass <- function(file, output = NULL, outdir = "dist") {
  if (is.null(output)) {
    output <- fs::path_file(file)
    fs::path_ext(output) <- "rstheme"
  }
  output <- fs::path(outdir, output)
  # cliapp::cli_alert_success("{path {fs::path_rel(file, getwd())}}")
  sass::sass(sass::sass_file(paste(file)), output = output)
}


get_theme_name <- function(file) {
  x <- readLines(file, warn = FALSE)
  x <- grep("rs-theme-name", x, value = TRUE)
  if (!length(x)) return("")
  x <- sub("^\\s*/\\*\\s*rs-theme-name:\\s", "", x)
  x <- sub("\\s*\\*/\\s*$", "", x)
  x
}

list_sassy_themes <- function(style = c("all", "light", "dark")) {
  if (!requireNamespace("rstudioapi", quietly = TRUE)) {
    stop("The {rstudioapi} package is required")
  }
  if (!rstudioapi::hasFun("getThemes")) {
    stop("Please upgrade RStudio to version 1.2+")
  }
  themes <- rstudioapi::getThemes()
  themes <- switch(
    match.arg(style),
    light = purrr::discard(themes, "isDark"),
    dark = purrr::keep(themes, "isDark"),
    themes
  )
  themes <- purrr::map_chr(themes, "name")
  themes <- themes[grepl("Sassy", themes)]
  unname(themes)
}

cp_to_rstudio <- function(file) {
  # cliapp::cli_alert("{arg \"{get_theme_name(file)}\"}")
  fs::file_copy(
    file, 
    fs::path_home_r(".R", "rstudio", "themes", fs::path_file(file)),
    overwrite = TRUE
  )
}

base16_make_theme <- function(palette_file) {
  base16_palette <- fs::path_file(palette_file)
  base16_info          <- base16_get_theme_info(palette_file)
  base16_attribution   <- base16_info$attribution
  base16_name          <- base16_info$name
  base16_rstudio_style <- base16_info$rstudio_style
  
  base16_template <- if (base16_info$isDark) {
    "base16-dark_template.scss"
  } else {
    "base16-light_template.scss"
  }
  
  base16_theme <- whisker::whisker.render(
    readLines(here::here("src", "base16", base16_template), warn = FALSE)
  )
  
  base16_theme_file <- sub("^_", "", base16_palette)
  # cliapp::cli_alert("Creating src/{arg {base16_theme_file}}")
  writeLines(base16_theme, fs::path(here::here("src", base16_theme_file)))
}

base16_get_theme_info <- function(palette_file) {
  # First and second comment lines of the palette_file must be
  # the base16 theme attribution (1) and the studio light/dark style (2)
  
  info <- list()
  base16_meta <- readLines(palette_file)
  base16_meta <- base16_meta[grepl("^\\s*/\\*", base16_meta)]
  info$attribution <- base16_meta[1]
  info$rstudio_style <- base16_meta[2]
  info$isDark <- grepl("is-dark: TRUE", info$rstudio_style, fixed = TRUE)
  if (info$rstudio_style == "") {
    info$isDark <- TRUE
    info$rstudio_style <- "/* rs-theme-is-dark: TRUE */"
  }
  rgx_name <- "/\\*\\s*([[:alpha:][:digit:][:punct:] ]+) by"
  info$name <- stringr::str_match(info$attribution, rgx_name)[1, 2]
  info
}

walk_sassy_themes <- function(style = c("all", "light", "dark"), auto = FALSE) {
  themes <- list_sassy_themes(style)
  for (theme in themes) {
    cliapp::cli_alert(theme)
    rstudioapi::applyTheme(theme)
    if (auto) {
      Sys.sleep(1)
    } else {
      res <- readline("Press enter for next theme or q to stop...")
      if (tolower(res) == "q") return()
    }
  }
}