# ---------- Movie Example --------------------------
ani.options(interval=0.2, nmax=nrow(DT.spotify_raw) * 32, imgdir=plots.p())

ylims <- range(c(DT.actual_and_expected$streams, DT.keyDates$streams)) * c(0.95, 1.05)
xlims <- DT.actual_and_expected[, range(activity_date)] + c(-2, +4)
title <- subtext(main="Orchard Streams on Spotify", subtext="World Wide Aug 2014 - Aug 2015")

### First plot all at solid line
### Then replot, except that second is faded

img_name <- "Spotify_Expected_Stream_Count_Big"
f.out_movie <- plots.p("animations", img_name, ext="gif", expand=TRUE)
img_name %<>% paste0("_", ., "_part")

lsize <- 0.9
dotsize <- 2.1
color_actuals <- "#07130D"

## save images and convert them to a single GIF
saveGIF({

    ## Start with a semi-blank canvas
    for (i in seq(11))
      print(ggLinegraph(DT.expected[FALSE], xlim=xlims, ylim=ylims, title=title, title_size_rel=2, legend="off", dotsize=dotsize, size=lsize) + scale_y_continuous(labels = function(yy) comma(yy/1e+06), limits=ylims) + ylab("Streams (in Millions)"))


    for (i in seq(nrow(DT.spotify_raw))) {
      ## Take out a few random frames
      if (i %in% c(3, 4, 16, 25, 31, 44:50, seq(100, 320, by=2)))
        next;

      V_actual <- NULL
      date_thresh <- DT.expected$activity_date[[i]] - 8
      vlines <- DT.keyDates[activity_date <= date_thresh & value_type == "actual streams", activity_date]
      if (length(vlines))
        V_actual <- geom_text(data=DT.keyDates[.(vlines)], aes(x=activity_date, y=streams, label=text), show.legend=FALSE, color="dark blue", size=3)

      P <- ggLinegraph(DT.expected[seq(i)], xlim=xlims, ylim=ylims, title=title, title_size_rel=2, legend="off", dotsize=dotsize, size=lsize, vline=vlines, vline_alpha=0.3) + V_actual

      ## STALL ONE TIME WHEN ADDING IN THE LINES
      if (any(DT.keyDates$activity_date == date_thresh))
        print (P)

      if (i < 5 || i %% 3 == 0)
        print (P)
      else 
        for (dummy in seq(0, max(0, 2 - floor(i / 25))))
          print(P)
    }

    ## Print the same graph twice, to stall a minute
    for (i in seq(7))
        print(P)

    ## HIGHLIGHT THE PART THAT IS FORECAST
    xlims.ann <- DT.expected[value_type == "forecasted streams", range(activity_date)] + c(-2, +2)
    ylims.ann <- DT.expected[value_type == "forecasted streams", range(streams)] * c(.95, 1.05)
    for (i in seq(12))
    print(P + annotate("rect", xmin=xlims.ann[[1]], xmax=xlims.ann[[2]], ymin=ylims.ann[[1]], ymax=ylims.ann[[2]], alpha = .1, color="blue") + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1, size=6.15, label="This is where Total Streams\nwere *EXPECTED* to be", fontface="bold", color="WHITE", vjust=1) + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1, size=6, label="This is where Total Streams\nwere *EXPECTED* to be", fontface="bold", color="#D42F4B", vjust=1))

    for (i in seq(7))
    print(P + annotate("rect", xmin=xlims.ann[[1]], xmax=xlims.ann[[2]], ymin=ylims.ann[[1]], ymax=ylims.ann[[2]], alpha = .1, color="blue") + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1, size=6.15, label="This is where Total Streams\nwere *EXPECTED* to be", fontface="bold", color="WHITE", vjust=1) + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1, size=6, label="This is where Total Streams\nwere *EXPECTED* to be", fontface="bold", color="#D42F4B", vjust=1) + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1/(8-i), size=6, label="This is where Total Streams\nwere *EXPECTED* to be\n\nHOWEVER ... ", fontface="bold", color="#D42F4B",vjust=1))

    for (i in seq(50))
    print(P + annotate("rect", xmin=xlims.ann[[1]], xmax=xlims.ann[[2]], ymin=ylims.ann[[1]], ymax=ylims.ann[[2]], alpha = .1, color="blue") + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1, size=6.15, label="This is where Total Streams\nwere *EXPECTED* to be", fontface="bold", color="WHITE", vjust=1) + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1, size=6, label="This is where Total Streams\nwere *EXPECTED* to be", fontface="bold", color="#D42F4B", vjust=1) + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1, size=6, label="This is where Total Streams\nwere *EXPECTED* to be\n\nHOWEVER ... ", fontface="bold", color="#D42F4B",vjust=1))

    for (i in seq(30))
    print(P + annotate("rect", xmin=xlims.ann[[1]], xmax=xlims.ann[[2]], ymin=ylims.ann[[1]], ymax=ylims.ann[[2]], alpha = .1, color="blue") + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1, size=6.15, label="This is where Total Streams\nwere *EXPECTED* to be", fontface="bold", color="WHITE", vjust=1) + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1, size=6, label="This is where Total Streams\nwere *EXPECTED* to be", fontface="bold", color="#D42F4B", vjust=1) + 
                annotate("text", x=diff(xlims.ann)/2 + xlims.ann[[1]], y=ylims.ann[[1]]-1.5e6, alpha = 1, size=6, label="This is where Total Streams\nwere *EXPECTED* to be\n\nHOWEVER ... ", fontface="bold", color="#D42F4B",vjust=1))

    for (i in seq(2))
    print(P + annotate("rect", xmin=xlims.ann[[1]], xmax=xlims.ann[[2]], ymin=ylims.ann[[1]], ymax=ylims.ann[[2]], alpha = .1, color="blue"))
    print(P + annotate("rect", xmin=xlims.ann[[1]], xmax=xlims.ann[[2]], ymin=ylims.ann[[1]], ymax=ylims.ann[[2]], alpha = .1))
                
    for (i in seq(2))
    print(P)
                

    vlines_f <- DT.keyDates[value_type != "actual streams", activity_date]
    T <- NULL
    for (vi in seq_along(vlines_f)) {
      V <- gg_vline(vline_value=as.numeric(vlines_f[seq(vi)]), vline_color="#CD0977", alpha=0.4)
      for (i in seq(5)) 
        print (P + V + T)
      T <- geom_text(data=DT.keyDates[.(vlines_f[seq(vi)])], aes(x=activity_date, y=streams, label=text), size=4.5, show.legend=FALSE, color="#900847", fontface="bold", alpha =1)
      for (i in seq(14)) 
        print (P + V + T)
    }

    ## Highight the expected values
    for (color in c(0:9, LETTERS[1:2])) {
      dict.colors.forecast <- rep(color, 3) %>% pasteC(5, sep="") %>% paste0("#",.) %>% c("actual streams" = color_actuals, "forecasted streams"=.)
      P.base <- ggLinegraph(DT.expected, xlim=xlims, ylim=ylims, title=title, title_size_rel=2, color="value_type", dict.colors=dict.colors.forecast, legend="off", dotsize=dotsize, size=lsize, vline=vlines, vline_alpha=0.3) + V + T + V_actual
      for (i in seq(4))
        print(P.base)
    }


    for (i in seq(10))
      print(P.base)

    for (i in seq(nrow(DT.actuals)))
        for (dummy in seq(0, max(0, 3 - floor(i / 20))))
          print(P.base + geom_line(data=DT.actuals[seq(i)], aes(x=activity_date, y=streams), color="#BE0000", size=lsize) + geom_point(data=DT.actuals[seq(i)], aes(x=activity_date, y= streams), color="#BE0000", size=dotsize))

    ## Print a few times for posperity
    for (i in seq(45))
      print(P.base + geom_line(data=DT.actuals, aes(x=activity_date, y=streams), color="#BE0000", size=lsize) + geom_point(data=DT.actuals, aes(x=activity_date, y=streams), color="#BE0000", size=dotsize))
# }, interval = 0.03, video.name = f.out_movie, img.name=img_name, ani.width = 1600, ani.height = 1200, clean=FALSE, other.opts="-r 1")
}, interval = 0.03, movie.name = f.out_movie, img.name=img_name, ani.width = 1600, ani.height = 1200, clean=FALSE)



file.exists(f.out_movie)
f.out_converted <- gsub(".gif$", ".mp4", f.out_movie)
sprintf("ffmpeg -r 15 -f gif -i %s %s", f.out_movie, f.out_converted) %>% system()
y