## parse_items.tracks_old.r

parse_items.tracks_old <- function(items, expected_fields=get_expected_fields("items"), paginate=TRUE
  , api.timestamp=items$api.timestamp, api.itemshref=items$href
  , token=get_spotify_API_token(), envir.token=parent.frame()
  , verbose.fetching=TRUE, verbose.writing=TRUE
) {
## This function is generally not called directly, but instead called from get_playlist_data

  ## Check that token is not expired; if so, refresh it
  token %<>% confirm_spotify_token(envir=envir.token)

  ## ----------------------------------- RECURSE IF PAGINATABLE -------------------------- ##
  ## If what was passed to the function is a paginat'able section (ie, "items" is a subgroup of what was passed)
  ## then call the function on the appropriate 'items' and also if paginate is TRUE, recurse
  if (check_expected_fields(items, url_type="pagination", quiet=TRUE)) {

    ## collect all the arguments that are the same for recursion. 
    ARGS <- collectArgs(except=c("items", "api.timestamp", "api.itemshref"))

    ret  <- do.call(parse_items.tracks, args=c(ARGS, list(items=items[["items"]], api.timestamp=api.timestamp, api.itemshref=api.itemshref)))
    # using do.call #   ret2 <- parse_items.tracks(items[["items"]], expected_fields=expected_fields, paginate=paginate, api.timestamp=api.timestamp, api.itemshref=api.itemshref, token=token, verbose.fetching=TRUE, verbose.writing=TRUE)
    # using do.call #   cat("ret & ret2", ifelse(identical(ret, ret2), "ARE", "ARE NOT"), "identical\n")

    next_url <- items$`next`
    if (paginate && !is.null(next_url)) {

      next_items <- get_json_from_spotify_url(next_url, token=token, envir.token=envir.token, verbose.fetching=verbose.fetching, verbose.writing=verbose.writing)
      next_ret  <- do.call(parse_items.tracks, args=c(ARGS, list(items=next_items, api.timestamp=next_items$api.timestamp, api.itemshref=next_url)))
      # using do.call #   next_ret2 <- parse_items.tracks(next_items, api.timestamp=next_items$api.timestamp, api.itemshref=next_url, expected_fields=expected_fields, paginate=paginate, token=token, verbose.fetching=TRUE, verbose.writing=TRUE)
      # using do.call #   cat("next_ret & next_ret2", ifelse(identical(next_ret, next_ret2), "ARE", "ARE NOT"), "identical\n")

      ## ERROR CHECK
      if (!check_expected_fields(next_items, url_type="pagination"))
        warning("issue caused when parsing url = ", next_url, call.=TRUE)
      if (!identical(names(ret), names(next_ret)))
        warning("names are not identical for ret & next_ret for url = ", next_url, call.=TRUE)

      ## RETURN THE rbound DT
      return(rbind(ret, next_ret, fill=TRUE))
    }
    return(ret)
  }
  ## ----------------------------------- RECURSE IF PAGINATABLE -------------------------- ##
  
  nms <- names(items)
  names(expected_fields) <- expected_fields

  ## ERROR CHECK
  if (!confirm_length_of_items(items, element="items"))
    stop("Stopping because items are not all of uniform length.  (What to do with the extra or missing rows?)")
  if (!check_expected_fields(items, expected_fields=expected_fields))
    stop("Stopping because items were not as expected and thus parsing will be off. (What to do with the extra or missing field?)")


  # items has four (4) elements. 
  # *  added_at  --- single column
  # *  added_by  --- <NEEDS EXPANDING>
  # *  is_local  --- single column
  # *  track     --- <NEEDS EXPANDING>


  ## added_by <- items$added_by
  ## -------------------------------------------
    ## Added by is generally which user added the track
    ## There are five fiedls.  Of those, three are repetetive
    ## ie,  uri, href and external_urls.   We are only keeping uris
    ## The other two can be deteremined from uri. 
    ## (presumably, we can determine id and type from uri too, but keeping those just in case)

    ## the added_by / added_at info moved to the bottom, when creating the final data.table

    ## not using href and external_urls
    # added_by.href <- items$added_by$href
    # added_by.external_urls <- items$added_by$external_urls

  ## track <- items$track
  ## -------------------------------------------

    ## --- TRACK > ALBUM ------ ##
    album_dropping <- c("images", "href", "external_urls")
    DT.track.album <- items$track$album %>% {.[names(.) %ni% album_dropping]} %>% as.data.table
    # DT.track.album[, c(album_dropping) := NULL]
    DT.track.album[, available_markets := sapply(available_markets, pasteC, C=",")]
    DT.track.album %<>% expand_sub_group(recursive=TRUE)
    DT.track.album %>% setnames(old="available_markets", new="markets")
    DT.track.album %>% setnames(old="album_type", new="producttype")
    DT.track.album %>% setcolorderpt(startCols=c("name", "producttype", "id", "uri"), endCols="markets")
    DT.track.album %>% setnames_addPreface("album", sep=".")


    ## --- TRACK > ARTIST ------ ##
    ## Artist is a tricky one, because a track can have multiple artists
    ## For each track in item$track, the $artist field is itself a list of indeterminate size (usually 1, but also 4 or 5 or more)
    ## Thus we will keep the URI as flattened CSV string
    ## NOT KEEPING FOR track$artist:   external_urls, href, type, uri

    ## Confirm that all artist_type is 'artist', since we are not keepting 'type'
    artist_type <- sapply(items$track$artist, function(x) unique(x$type)) 
    if (!all(artist_type == "artist") || any(is.na(artist_type)))
        warning ("There was an example of items$track$artist != 'artist'\t -- The found value(s) was '", pasteC(setdiff(artist_type, "artist"), C=", "), "'", call.=FALSE)
    ## Confirm that all of the URIs are of the form  'spotify:artist:<id>'
    condition_to_test <- sapply(items$track$artist, function(x) all(x$uri == sprintf('spotify:artist:%s', x$id)))
    if (!all(condition_to_test, na.rm=TRUE))
        warning ("There was an example of items$track$artist where the URI was *not* of the form    spotify:artist:<id>", call.=FALSE)
        ## use this next line for debugging, if any such exceptions are found
        # lapply(items$track$artist, function(x) data.table(x$uri , sprintf('spotify:artist:%s', x$id))) %>% rbindlist %>% "["(V1 != V2)


    DT.track.artist <- rbindlist(lapply(items$track$artist, function(row)
        data.table(namecsv = vec_to_csv(row$name), idcsv = vec_to_csv(row$id), numberof=length(row$id))))
    DT.track.artist %>% setnames_addPreface("artist", sep=".")


  data.table(
    ## start with external_ids -- ie ISRC
     expand_sub_group(items$track$external_ids) %>% as.data.table
   , name            =   valueIfErr( items$track$name,         "")
   , id              =   valueIfErr( items$track$id,           "")
   , type            =   valueIfErr( items$track$type,         "")
   , uri             =   valueIfErr( items$track$uri,          "")
   , datetimeadded   =   valueIfErr( items$added_at,           "")
   , DT.track.artist
   , DT.track.album
   , markets = vec_to_csv(items$track$available_markets)

   , islocal         =   valueIfErr( items$is_local,           NA)

   , addedby.id      =   valueIfErr( items$added_by$id,        "")
   , addedby.type    =   valueIfErr( items$added_by$type,      "")
   , addedby.uri     =   valueIfErr( items$added_by$uri,       "")

   , discnumber      =   valueIfErr( items$track$disc_number,  NA_integer_)
   , durationms      =   valueIfErr( items$track$duration_ms,  NA_integer_)
   , explicit        =   valueIfErr( items$track$explicit,     NA)
   # , href          =   items$track$href
   , popularity      =   valueIfErr( items$track$popularity,   NA_integer_)
   , previewurl      =   valueIfErr( items$track$preview_url,  "")
   , tracknumber     =   valueIfErr( items$track$track_number, NA_integer_)

    ## Add in the URL, for quick reference in case we need to repull the info or for reference to the file saved on disk
   , api.itemshref =   valueIfNull(api.itemshref, "")
   , api.timestamp =   valueIfNull(api.timestamp, "")
  ) %>% setnames_addPreface("track", sep=".")
} ## // close function
