# Execution of Plot_Users_By_Country_and_Product.r

# &&& Add to ggsave.out  createifnotexists

lib(ggplot2,  quietly=TRUE)
lib(reshape2, quietly=TRUE)
lib(scales)

EXECUTE <- TRUE

## EXECUTION

## CREATE P.heat
if (EXECUTE) 
{

  setScienceIfNot("Spotify_Accounting_ETL", load=TRUE)
  sourceSupportFns(proj="Spotify_Streams_Per_Top_Artist")
  loadIfNotExists("DT.userCounts")
  loadIfNotExists("DT.country")

  max_month <- DT.userCounts[, max(activity_month)]
  cat("\n\n")

  latestDate <- max(DT.userCounts$activity_month)
  measureCols <- c("Premium", "AdFunded", "Total")
  products_using <- c("A", "P")


  DT.Latest_Active_Users <- dcast.data.table(DT.userCounts[activity_month == latestDate & product %in% products_using][product == "A", product_description := "AdFunded"], formula="country_code ~ product_description", value.var=c("active_users"))
  DT.Latest_Registered_Users <- dcast.data.table(DT.userCounts[activity_month == latestDate & product %in% products_using][product == "A", product_description := "AdFunded"], formula="country_code ~ product_description", value.var=c("registered_users"))

  DT.Latest_Registered_Users[, Total := removeNA(AdFunded, 0) + removeNA(Premium)]
  DT.Latest_Active_Users[, Total := removeNA(AdFunded, 0) + removeNA(Premium)]
  setcolorderpt(DT.Latest_Active_Users,    endCols=measureCols)
  setcolorderpt(DT.Latest_Registered_Users, endCols=measureCols)

  DT.Latest <- merge(DT.Latest_Active_Users, DT.Latest_Registered_Users, by=c("country_code"), all=TRUE, suffix=c("_Active", "_Regstrd"))

  DT.Latest <- DT.Latest[, lapply(.SD, function(x) round(x / 1e6, ifelse(x>1e4, 2, 4))), keyby=country_code]

  addCountry.byCode_(DT.Latest)
  DT.Latest[country_code == "TW", country_name := "Taiwan"]
  DT.Latest[order(Premium_Active, decreasing=TRUE)][1:50]

  ## Redo, given DT.Latest_Active_Users & DT.Latest_Registered_Users
  {
    ## CONFIRM: there are no missing countries from either table
    stopifnot(identical(unique(DT.Latest_Active_Users$country_code),unique(DT.Latest_Registered_Users$country_code)))

    N.top <- 30
    countryOrder <- DT.Latest_Active_Users[order(Premium, decreasing=TRUE), country_code]

    DT.Latest_Active_Users[     , country_code := factor(country_code, levels=countryOrder)]
    DT.Latest_Registered_Users[ , country_code := factor(country_code, levels=countryOrder)]

    P.active <- ggHeatColumns(DT.Latest_Active_Users, id.var="country_code"
                  , title="Active Users (in millions)"
                  , divideBy=1e6, top=N.top, axis.include=TRUE)
    P.registered <- ggHeatColumns(DT.Latest_Registered_Users, id.var="country_code"
                  , title = "Registered Users (in millions)"
                  , divideBy=1e6, top=N.top, axis.include=TRUE)

    sub.txt <- "\nOnly user count for primary products are shown -- bundled products are excluded."
    sub.exp <- sprintf('expression(atop("%s", atop(italic("%s"), "")))', sub.txt, gsub("\\n", "", orchardFootNote()$label))
    sub <- gg_text(eval(parse(text=sub.exp)), .8)

    P.heat <- arrangeGrob(P.active, P.registered, sub=sub, nrow=2, heights=c(10, 10))
  }
}

## CREATE Plots by country group
if (EXECUTE) 
{
  products_using = c("P", "A")

  DT.country <- get_dim_country(refresh=FALSE)

  ## 2015-01-27 Adding country groups to get_dim_country()
  group_order <- c("North America", "Scandinavia", "Western Europe", "GSA", "BeNeLux", "Asia", "Australia", "Latin America", "Balkans")
  
  if (!exists("DT.country"))
    DT.country <- get_dim_country(assign=FALSE)

  # DT.country <- DT.country[, list(country_code, country_name)]
  # DT.country[country_code %in% c("SE", "NO", "DK", "FI"), region_group := "Scandinavia"]
  # DT.country[country_code %in% c("BE", "NL", "LU"), region_group := "BeNeLux"]
  # DT.country[country_code %in% c("DE", "CH", "AT"), region_group := "GSA"]
  # DT.country[country_code %in% c("GB", "ES", "FR", "IT", "IE"), region_group := "Western Europe"]
  # DT.country[country_code %in% c("AR", "BR", "MX", "UY", "CO", "CL"), region_group := "Latin America"]
  # DT.country[country_code %in% c("PH", "SG", "TW", "HK", "MY"), region_group := "Asia"]
  # # DT.country[country_code %in% c("HR", "RO", "RS", "ME", "AL", "BA", "SI"), region_group := "Balkans"]
  # DT.country[country_code %in% c("US", "CA"), region_group := "North America"]
  # DT.country[country_code %in% c("AU", "NZ"), region_group := "Australia"]

  # DT.country[, region_group := factor(region_group, levels=group_order)]

  # ## Clean up
  # DT.country[, country_code := setFactorOrder(country_code, ordering=countryOrder)]
  # DT.country[, country_name := factor(country_name, levels=unique(country_name[order(country_code)]))]

  region_group_to_factor_(DT.country)
  setkeyIfNot(DT.country, "region_group", verbose=FALSE, warnForColNameInEnv=FALSE)

  PLOTS.by_region_group <- list()

  for (r_g in removeNA(unique(DT.country$region_group))) {
    cat("Processing ", r_g, "\n")
    PLOTS.by_region_group[[r_g]] <- Plot_UserCount_forCountryGroup(countries_using = DT.country[.(r_g)]$country_code, region_group=r_g, products_using=products_using)
    # ggsave.out(plot=PLOTS.by_region_group[[r_g]], nm = r_g, height=12, footnote=NULL, subFolder="UserCounts_by_Country")
  }
} ## // end if(FALSE)

if (EXECUTE)
{
  f.name <- sprintf("Spotify User Counts as of %s", format(max_month, "%b %Y"))
  f.out <- out.p(f.name, ext="pdf")
  pdf(width=12, height=10, file=f.out, onefile=TRUE, title=f.name)
  # print(P.heat)
  # print(PLOTS.by_region_group)
  grid.draw(P.heat)
  for (P in PLOTS.by_region_group)
    grid.draw(P)
  dev.off()
  reveal(f.out)
  .o(f.out)
}
"------------------------------------------"
