# setScience("accel")
# setGitBranchToSystem(); .g()

# sfShowWarehouses()
# wh <- getWH_already_on(default="spotifyaggregates")
# wh <- "spotifyaggregates"
# dbname <- "prod"

# setSnowflake(wh=wh, dbname=dbname)

# tbl <- "by_main_isrc"
# tbl <- "by_main_isrc_country_code"

# DT.sample <- sfQry(makeQry(colsToPull="*", expand=FALSE, schema="spot_stream_counts", tbl=tbl, main_isrc=get_sample_isrcs(1), minDate=today()-120, dateCol="weekof"))
# DT.meta <- getISRCMeta(get_sample_isrcs(0), drop_parent=FALSE, aggregate_by_isrc=TRUE)


# #|    streams   is_up_or_down := sign(streams accel score);    Describes if streams have gone up this week compared to last week
# #|    metascore is_up_or_down := sign(metascore accel score);  Describes if accel of streams have gone up this week compared to last week
# #|    
# #|    ## for is_up_or_down
# #|    When stream and metascore is_up_or_down defer, that means that there is a change coming
# #|    If streams is positive and metascore is negative, that means it is slowing down
# #|    If metascore is negative and streams is positive, that means that the decline is slowing down (and thus leveling off)
# #|    
# #|    The "yellow lines" occur when the streams_accel_score_scaled is HIGH (ie, above 0.90, 0.905) and at the same time the metascore is up
# #|    this means that not only was there a large increase in streams, but there was a large increase in the increase


# open <- TRUE

# if (FALSE) {
#   DT.sample <- fread("/Users/rsaporta/Downloads/2015_Fall/result (3).tsv", na.string="NULL")
#   cleanColNamesForSQL_(DT.sample)
# }


# if (!is.Date(DT.sample$weekof)) {
#   if (!any(is.na(as.Date(DT.sample$weekof))))
#     DT.sample[, weekof := as.Date(weekof)]
#   DT.sample[, weekof := as.Date(weekof, format="%m/%d/%y")]
# }

# ## do this right after pulling the data
# if (.Pfm != "Darwin") {
#   if ("main_isrc" %in% names(DT.sample) && "isrc" %ni% names(DT.sample))
#   DT.sample[, isrc := main_isrc]
#   if (FALSE) DT.sample[, setdiff(names(DT.meta), "isrc") := NULL]
#   addColsFrom_(DT.sample, DT.meta, joinCols="isrc")

#   jesusForData(DT.sample)
# }


# &&& TODO:
# Get the full list of Releases it appears on and add this as a table
# "Apears on: "


open_first <- TRUE
isrcs.all <- DT.sample[, unique(isrc)]
# isrcs.all %<>% head(3)
f.out <- emptylist(isrcs.all)
Plots.list <- emptylist(isrcs.all)
for (isrc_using in isrcs.all) 
{

  DT.plot <- DT.sample[weekof != max(weekof)] [isrc %in% isrc_using]
  DT.plot[, "|" := "|"]
  DT.plot[, sprev := shiftUp(metascore_is_up_or_down), by=isrc]
  DT.plot[, rprev := shiftUp(streams_is_up_or_down), by=isrc]
  DT.plot[, scores.lab := (fwp(streams_accel_score_scaled, 1, symbol="")), by=isrc]

  DT.plot[, YESYES := (shiftUp(streams_accel_score_scaled) > 0.9) & (sprev == 1 | .N <=3), by=isrc]

  if ("country_code" %in% names(DT.plot)) {
    DT.plot[, include_daily := streams >= (mean(streams) + sd(streams)), by=weekof]
    DT.plot[, include := any(include_daily), by=country_code]
    DT.plot[country_code=="US", include := TRUE]
    if (DT.plot[1, release_genre] == "Latin")
        DT.plot[country_code %in% c("MX", "ES"), include := TRUE]
    if (DT.plot[(include), lunique(country_code) < 5])
      DT.plot[country_code %in% c("GB", "MX"), include := TRUE]
    if (DT.plot[(include), lunique(country_code) < 5])
      DT.plot[country_code %in% c("AR", "BR"), include := TRUE]
    DT.plot <- DT.plot[(include)]
  }

  ## organize according to stream count
  DT.plot[, meann(streams), by=country_code]

  if (is.na(DT.plot[1, release_name])) {
    Title <- sprintf("History for track%s  %s", ifelse(length(isrc_using) > 1, "s", ""), commaSep(isrc_using))
  } else {
    ## Original
    Title <- DT.plot[1, sprintf("%s: \"%s\" [%s]\n %s / %s%s | %s (%s%s)\n %s (%s) | %s\n%s", artist_name, track_name, release_genre, upc, isrc, ifelse(N_by_isrc>1, sprintf(" (%s dups)", N_by_isrc), ""), release_name
      , ifelse(release_sale_start_date == release_date, "", format(release_date, format="%b-%d-%Y / "))
      , format(release_sale_start_date, format="%b-%d-%Y")
      , label_name, labelid, client_manager, removeNA(track_url, ""))]
    Title2 <- strsplit(Title, "\n\\s*") %>% unlist %>% sapply(trim) %>% {.[1:4]} %>% {subtext(  .[[1]], .[[2]], .[[3]], .[[4]], body.subtext="plain", body.main="bold"  )}
  }

  # if (identical(isrc_using, "BRWRT1400042"))
  #   Title <- sprintf("Som Livre | Wesley Safadão | \"Camarote\" (BRWRT1400042)")
  # if (identical(isrc_using, "GBPS81500025"))
  #   Title <- sprintf("Ameritz Music | Christmas Choir | \"Oh Christmas Tree\" (GBPS81500025)")

  P.accel_score_example <- 
  {
    (ggLinegraph(DT.plot, x="weekof", y="streams", color="country_code", dotsize.scale=2, facet_y=rev(c("isrc")), x_angledtext=15, x_relativetext=.9, title=NULL, label="scores.lab", ymin=0, labelcolor=c("white", "black"), labelalpha=c(1,.85), xlab="") + scale_x_date(breaks = date_breaks("week"), date_labels="%b-%d")) %>%
        ggInsertLayer(after=1,   gg_vline(alpha=1, color="yellow", vline_value=DT.plot[(YESYES), as.numeric(weekof)], linetype="4C88C488")) %>%
        ggInsertLayer(after=2,   gg_vline(alpha=1, color="red", vline_value=DT.plot[sprev ==1, as.numeric(weekof)], linetype="1F")) %>%
        ggInsertLayer(after=1,   gg_vline(alpha=.25, color="blue", vline_value=DT.plot[rprev==1, as.numeric(weekof)], linetype="solid"))
  }


  cols_main <- c("country_code", "weekof", "streams", "streams_accel_score_scaled", "streams_is_up_or_down", "streams_upup", "streams_upupup", "|", "streams", "metascore_accel_score_scaled", "metascore_is_up_or_down", "metascore_upup", "metascore_upupup", "YESYES")

  ## Keep it a bit more professional
  cols_main %<>% setdiff(., c("|", "YESYES")) %>% unique

  ## PLOT TEXT
  ## ----------------------------- ##
      theme.smaller_font <- ttheme_default(
      core=list(bg_params = list(col=NA),
                fg_params=list(fontsize=7)), 
      colhead=list(fg_params=list(col="navyblue", fontsize=6)),
      rowhead=list(fg_params=list(col="navyblue", fontface=2L)))

      theme.smaller_font <- ttheme_minimal(
      core=list(bg_params = list(col=NA, fill=blues9[c(1,2)]),
                fg_params=list(fontsize=7)), 
      colhead=list(fg_params=list(col="navyblue", fontsize=6), bg_params=list(fill="gray")),
      rowhead=list(fg_params=list(col="navyblue", fontface=2L)))

      DT.text <- DT.plot[weekof >= max(weekof)-30, lapply(.SD, function(x) if (is.numeric(x)) if (meann(abs(x)) < 1) round(x, 3) else formnumb(x, round=2) else x), .SD=cols_main]
      # DT.text <- DT.plot[, list(streams=sum(streams)), keyby=weekof]
      setkey(DT.text, country_code, weekof)
      DT.text[, weekof := format(weekof, format="%b-%d")]
      setnames(DT.text, gsub("_", "\n", names(DT.text)) %>% gsub("is\\nup\\nor", "is up or", .))
      setnames(DT.text, gsub("upup", "2x +trend", gsub("upupup", "3x +trend", names(DT.text))))
      P.text <- gridExtra::tableGrob(DT.text, rows = NULL, theme=theme.smaller_font)
      P.text$widths <- unit(rep(1/ncol(P.text), ncol(P.text)), "npc")
      P.text$heights <- unit(rep(6/nrow(P.text), nrow(P.text)), "inches")
  ## ----------------------------- ##


  # warning("dont forget to clean the name better")
  # f.name <- pasteC(c("NEW_UPUP__accel_score_example", isrc_using), C="_")
  # f.out[[isrc_using]] <- printToPDF(list(P.accel_score_example, P.text), f.name=f.name, height.per.plot=5, width=12, open.when.done=open_first, main=Title2)
  # open_first <- FALSE

  Plots.list[[isrc_using]] <-   arrangeGrob(P.accel_score_example, P.text, ncol=1, nrow=2, top=textGrob(Title2), bottom=orchardFootNote(), widths=c(8), heights=c(7, 14))
}

f.out <- printToPDF(Plots.list, f.name=timeStamp("Tracks Spiking on Spotify"), height=12, width=12, paginate=TRUE, theme=ttheme_minimal())

if (isTRUE(open) || is.function(open)) {
  .o(unlist(f.out))
}