
projName <- "MktingReport"
wrkDir <- "~/git/orch"
setScience(wrkDir=wrkDir, project=projName, subl=FALSE)

frmt.monthday_time <- "%m%d_%H%M"
source(as.path(srcDir, "Funcs.r"))

fbAdds.csv <- as.path(dataDir, "Ad Campaigns Results - Facebook Ads - 2013.csv")

Years <- c(as.character(2011:2013))
fbAdds.csv <- as.path(dataDir, paste0("Ad Campaigns Results - Facebook Ads - ", Years, ".csv"))
names(fbAdds.csv) <- Years

fbList <- list()
for (Y in Years) {

  fbAdds <- read.table(fbAdds.csv[[Y]], sep=",", quote="\"", stringsAsFactors=FALSE)

  # Remove blank rows and also convert to data.table
  fbAdds <- data.table( removeBlankRows(fbAdds) )
  
  # Use the first Row as the column names
  nms <- unlist(fbAdds[1])
  nms[[1]] <- "Info"
  setnames(fbAdds, nms)

  ## ---------------------------------------------- ##
  ##  Identify the groups.  
  ##     Splice out the Name/Date field
  ## ---------------------------------------------- ##
    # label the groups
    fbAdds[ ,  row := seq_len(nrow(fbAdds))]
    fbAdds[Impressions == "Impressions", Group := seq_len(.N)]
    fbAdds[is.na(Group), Group := 0L]
    fbAdds[, Group := factor(cumsum(Group))]  
    # The actual Group label is arbitrary

    DateCols <- c("DateFrom", "DateTo")
    fbAdds[, c("Name", DateCols) := parseNameDate(Info[[1]]), by=Group]
    fbAdds[, c(DateCols) := lapply(.SD, as.Date, origin="1970-01-01"), .SDcols=DateCols]
  ## ---------------------------------------------- ##

  ## ---------------------------------------------- ##
  ## Now we need to chop up by Info group. 
  ## ---------------------------------------------- ##
      fbAdds[, c("Type", "Notes") := BannerType(Info)]
  ## ---------------------------------------------- ##

  ## ---------------------------------------------- ##
  ##  Drop the rows & columns no longer needed      ##
  ## ---------------------------------------------- ##
      ##  all header Rows
      fbAdds <- fbAdds[Impressions != "Impressions"]

      ## TODO:  Confirm this.  I'm fairly certain this is correct, but not positive
      ## Banner Totals / Weighted Averages is just the row totals.  But each artist date range has only one row
      fbAdds <- fbAdds[Info != "Banner Totals / Weighted Averages"]

      ##   row number. Group label (now Name & Date form the Group)
      fbAdds[, c("Info", "Group", "row") := NULL]
  ## ---------------------------------------------- ##



  ## ---------------------------------------------- ##
  ##  Clean up the data.table structure             ##
  ## ---------------------------------------------- ##
    # set the keys
    setkey(fbAdds, Name, DateFrom, DateTo)
    setcolorderpt(fbAdds, endcols=c("Type", "Notes"))
  ## ---------------------------------------------- ##



  ## -------------------------------------------------- ##
  ##  Clean up numeric columns (removing string parts)  ##
  ## -------------------------------------------------- ##
    # Identify which columns need which cleaning
    NumbCols <- c("Impressions", "Clicks", "Actions")
    CurCols  <- c("CPM", "CPC", "Total Spent")
    PercCols <- c("CTR")

    fbAdds[, c(NumbCols) := lapply(.SD, commaToNumeric), .SDcols=NumbCols]
    fbAdds[, c(CurCols)  := lapply(.SD, currToNumeric),  .SDcols=CurCols]
    fbAdds[, c(PercCols) := lapply(.SD, percToNumeric),  .SDcols=PercCols]
  ## -------------------------------------------------- ##

  ## Add a column for the year
  fbAdds[, "Year" := as.numeric(Y)]

  fbList[[Y]] <- fbAdds

}

# Confirm that there are no factors before rbindlist'ing
stopifnot(any(are(fbList[[1]]) != "factor"))

keys <- key(fbList[[1]])
fbAdds <- rbindlist(fbList)
setkeyv(fbAdds, c("Year", keys))
setcolorderpt(fbAdds)
fbAdds[, Year := as.factor(Year)]
