## Spotify


  ## MANUALLY PICK ONE
  nm.rfmc <- "Spotify"
  rfmc <- rfmcs[[nm.rfmc]]
  # DT.spot_review <- copy(DT.spot_review.spot)


# setnames(DT.spot_review, "adsupported_vs_premium.1", "spotify_adsupported_vs_premium")
kCols <- names(DT.spot_review.spot) %>% head(7)
setkeyIfNot(DT.spot_review.spot, kCols, verbose=FALSE)

## Crop to maxMonth; because the original data is a join of fact_sales data with SPOTIFYMGMT.REVSHARE and the latter is ahead of fact_sales
DT.spot_market_share.spot <- DT.spot_review.spot[spotify_activity_month < maxMonth]

measureCols <- nwhich(canBeNumeric(DT.spot_market_share.spot)) %>% setdiff(rfmc)
dimCols <- c("activity_month", "adsupported_vs_premium", rfmc)
dimCols_no_fmc <- setdiff(dimCols, rfmc)

## NA's exist due to a mismatch between fact_sales and SPOTIFYMGMT.REVSHARE
## SEE HOW MANY $ THE NA'S REPRESENT
if ("payable_to_orchard_usd" %in% names(DT.spot_market_share.spot)) {
  ## Show User what will be dropped
  catn("This is the value (in gross USD) of the rows of data that will be dropped (ie, for NA's introduced where fact_sales and SPOTIFYMGMT.REVSHARE did not match")
  DT.spot_market_share.spot[, lapply(.SD, sumn), keyby=list("Will Be Dropped"=is.na(activity_month) | is.na(payable_to_orchard_usd)), .SDcols=c("spotify_gross_revenue_usd", "payable_to_orchard_usd", "gross_revenue_usd")] %>% formnumb(round=0) %>% print

  ## Drop where either payable_to_orchard_usd was NA or activity_month is NA
  DT.spot_market_share.spot <- DT.spot_market_share.spot[!is.na(activity_month) & !is.na(payable_to_orchard_usd)]
} else {
  ## THE NA'S ARE NEGLIGIBLE;  DROP THEM
  DT.spot_market_share.spot <- DT.spot_market_share.spot[!is.na(activity_month)]
}


DT.spot_market_share.spot.quick_agg <- DT.spot_market_share.spot[activity_month >= minMonth, lapply(.SD, sumn), .SDcols=measureCols, keyby=list(activity_month, release_front_mid_or_cat_spotify_definition)]
### SLOPPY:  Needs to compute percentages by streams_fact_sales
DT.spot_market_share.spot.quick_agg[, marketshare_by_group := streams_fact_sales / spotify_total_streams]
DT.spot_market_share.spot.quick_agg[(month(activity_month) == month(max(activity_month))), text := fwp(marketshare_by_group)]

setSpotifyFMCtoFactor(DT.spot_market_share.spot.quick_agg)

Title <- subtext("Marketshare by Frontline vs Catalog Group", "Using Revised Definition")
P.marketshare_by_group_reviseddef <- ggLinegraph(DT.spot_market_share.spot.quick_agg, color="release_front_mid_or_cat_spotify_definition", y="marketshare_by_group", label="text", title=Title, legend="top", legend_title_on=FALSE, margin_right=2, margin_scale=7, ylims=c(0, 0.0525))

# ggsave.out(P.marketshare_by_group_reviseddef, open=TRUE, footnote=FALSE)
# reveal(ggsave.out)

# -------------------------------------------------------- --------------------------------------------------------

## Orchard


  ## MANUALLY PICK ONE
  nm.rfmc <- "Orchard"
  rfmc <- rfmcs[[nm.rfmc]]
  DT.spot_review <- copy(DT.spot_review.orch)



# setnames(DT.spot_review.orch, "adsupported_vs_premium.1", "spotify_adsupported_vs_premium")
kCols <- names(DT.spot_review.orch) %>% head(7)
setkeyIfNot(DT.spot_review.orch, kCols, verbose=FALSE)

## Crop to maxMonth; because the original data is a join of fact_sales data with SPOTIFYMGMT.REVSHARE and the latter is ahead of fact_sales
DT.spot_market_share.orch <- DT.spot_review.orch[spotify_activity_month < maxMonth]

measureCols <- nwhich(canBeNumeric(DT.spot_market_share.orch)) %>% setdiff(rfmc)
dimCols <- c("activity_month", "adsupported_vs_premium", rfmc)
dimCols_no_fmc <- setdiff(dimCols, rfmc)

## NA's exist due to a mismatch between fact_sales and SPOTIFYMGMT.REVSHARE
## SEE HOW MANY $ THE NA'S REPRESENT
if ("payable_to_orchard_usd" %in% names(DT.spot_market_share.orch)) {
  ## Show User what will be dropped
  catn("This is the value (in gross USD) of the rows of data that will be dropped (ie, for NA's introduced where fact_sales and SPOTIFYMGMT.REVSHARE did not match")
  DT.spot_market_share.orch[, lapply(.SD, sumn), keyby=list("Will Be Dropped"=is.na(activity_month) | is.na(payable_to_orchard_usd)), .SDcols=c("spotify_gross_revenue_usd", "payable_to_orchard_usd", "gross_revenue_usd")] %>% formnumb(round=0) %>% print

  ## Drop where either payable_to_orchard_usd was NA or activity_month is NA
  DT.spot_market_share.orch <- DT.spot_market_share.orch[!is.na(activity_month) & !is.na(payable_to_orchard_usd)]
} else {
  ## THE NA'S ARE NEGLIGIBLE;  DROP THEM
  DT.spot_market_share.orch <- DT.spot_market_share.orch[!is.na(activity_month)]
}


DT.spot_market_share.orch.quick_agg <- DT.spot_market_share.orch[activity_month >= minMonth, lapply(.SD, sumn), .SDcols=measureCols, keyby=list(activity_month, release_front_mid_or_cat)]
### SLOPPY:  Needs to compute percentages by streams_fact_sales
DT.spot_market_share.orch.quick_agg[, marketshare_by_group := streams_fact_sales / spotify_total_streams]
DT.spot_market_share.orch.quick_agg[(month(activity_month) == month(max(activity_month))), text := fwp(marketshare_by_group)]

DT.spot_market_share.orch.quick_agg[release_front_mid_or_cat == "Frontline", release_front_mid_or_cat := "Frontline_0_to_2"]
DT.spot_market_share.orch.quick_agg[release_front_mid_or_cat == "Midline", release_front_mid_or_cat := "Midline_2_to_18"]
DT.spot_market_share.orch.quick_agg[release_front_mid_or_cat == "Catalog", release_front_mid_or_cat := "Catalog_18_plus"]

setSpotifyFMCtoFactor(DT.spot_market_share.orch.quick_agg)

Title <- subtext("Marketshare by Frontline vs Catalog Group", "Using Classic Definition")
P.marketshare_by_group_classicdef <- ggLinegraph(DT.spot_market_share.orch.quick_agg, color="release_front_mid_or_cat", y="marketshare_by_group", label="text", title=Title, legend="top", legend_title_on=FALSE, margin_right=2, margin_scale=7, ylims=c(0, 0.05))

# ggsave.out(P.marketshare_by_group_classicdef, open=TRUE, footnote=FALSE)



## ~~~~~~~~~~~~~~~~~~~~~~~~~~ UPDATE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
# combine the two into one

DT.plot <- 
rbind(
   setnames(copy(DT.spot_market_share.orch.quick_agg)[, definition := "CLASSIC (catalog is 19+)"], "release_front_mid_or_cat",                    "rfmc")
 , setnames(copy(DT.spot_market_share.spot.quick_agg)[, definition := "UPDATED (catalog is 49+)"], "release_front_mid_or_cat_spotify_definition", "rfmc")
)

DT.plot[, rfmc := as.character(rfmc)]
vals.rfmc <- DT.plot[, as.character(unique(rfmc))]
DT.plot[rfmc %in% extract("Front", vals.rfmc), rfmc := "Frontline"]
DT.plot[rfmc %in% extract("Midl", vals.rfmc), rfmc := "Midline"]
DT.plot[rfmc %in% extract("Cata", vals.rfmc), rfmc := "Catalog"]
setSpotifyFMCtoFactor(DT.plot)
DT.plot$rfmc

xlims <- c(minMonth, maxMonth) %>% as.Date %>% {. + c(-3, 3)}


subtitle <- "CLASSIC: 0~2, 3~18, 19+        UPDATED: 0~3, 3~48, 49+"
Title <- subtext("", "Marketshare by Varying Definitions of Frontline/Midline/Catalog", subtitle)
P.marketshare_by_definition <- 
  ggLinegraph(DT.plot, facet_x="definition", color="rfmc", y="marketshare_by_group", label="text", label_y_up=0.08, title=Title, legend="top", legend_title_on=FALSE, margin_right=1.8, margin_bottom=0.5, margin_top=0.5, margin_scale=4, ylims=c(0, 0.0525), xlims=xlims, xlab="", relativetext=0.7, y_relativetitle=0.6)

ggsave.out(P.marketshare_by_definition, powerpoint=TRUE, open=TRUE, width=7, height=4, units="in")

