#!/usr/bin/env node

/* Writes files for JSON representation of all individuals on tracks with a given individual */

var EventEmitter = require("events").EventEmitter;
var argv = require("minimist")(process.argv.slice(2));
var fs = require("graceful-fs");

var logger = new EventEmitter();

if (! argv.q) {
  logger.on("log", function (msg) {
    console.log(msg);
  });
}

var sourceDataDir = __dirname + "/../node_modules/music-routes-data/data";
var individuals = require(sourceDataDir + "/individuals.json");
var individualIds = individuals.map(function(elem) { return elem._id; });
var individual_track = require(sourceDataDir + "/individual_track.json");
var tracks = require(sourceDataDir + "/tracks.json");
var releases = require(sourceDataDir + "/releases.json");
var track_release = require(sourceDataDir + "/track_release.json");

var getName = function (id) {
  return individuals.filter(function(elem) {
    return elem._id === id;
  })[0].names[0];
};

var generateJson = function (individualId) {
  logger.emit("log", "Generating JSON data for individual with ID of " + individualId + "...");

  var sourceLabel = getName(individualId);

  var tracksWithIndividual = individual_track.filter(function (elem) {
    return elem.individual_id === individualId;
  }).map(function (elem) {
    return elem.track_id;
  });

  var tracksDetailsForIndividual = tracks.filter(function (elem) {
    return tracksWithIndividual.indexOf(elem._id) > -1;
  });

  tracksDetailsForIndividual.forEach(function (track) {
    var trackReleaseIds = track_release.filter(function (relation) {
      return relation.track_id === track._id;
    }).map(function(elem) { return elem.release_id; });

    track.releases = releases.filter(function (release) {
      return trackReleaseIds.indexOf(release._id) > -1;
    });

  });

  var trackCounts = {};
  var trackArrays = {};
  var connectedIndividuals = individual_track.filter(function (elem) {
    if (tracksWithIndividual.indexOf(elem.track_id) > -1) {
      if (elem.individual_id != individualId) {
        if (trackCounts[elem.individual_id]) {
          trackCounts[elem.individual_id]++;
          trackArrays[elem.individual_id].push(elem.track_id);
          return false;
        }
        trackCounts[elem.individual_id] = 1;
        trackArrays[elem.individual_id] = [elem.track_id];
        return true;
      }
    }

    return false;
  }).map(function (elem) {
    return {
      name: getName(elem.individual_id),
      targetId: elem.individual_id,
      trackCount: trackCounts[elem.individual_id],
      tracks: trackArrays[elem.individual_id]
    };
  });

  var file = individualId + ".json";
  var formattedOutput = JSON.stringify({
    source: sourceLabel,
    trackCount: tracksWithIndividual.length,
    tracks: tracksDetailsForIndividual,
    targets: connectedIndividuals
  }, null, 2);

  fs.writeFile(__dirname + "/../data/" + file, formattedOutput,
    function (err) {
      if (err) {
        throw err;
      }
      logger.emit("log", "Wrote JSON file for individual with ID of " + individualId);
    }
  );
};

individualIds.forEach(function (individualId) {
  generateJson(individualId);
});

@@ -834,16 +834,138 @@
 .json%22);
+%0Avar artists = require(sourceDataDir + %22/artists.json%22);%0Avar artist_track = require(sourceDataDir + %22/artist_track.json%22);
 %0A%0Avar ge
@@ -1760,32 +1760,33 @@
  %7D).map(function
+ 
 (elem) %7B return 
@@ -1930,16 +1930,304 @@
    %7D);%0A%0A
+    var trackArtistIds = artist_track.filter(function (relation) %7B%0A      return relation.track_id === track._id;%0A    %7D).map(function (elem) %7B return elem.artist_id; %7D);%0A%0A    track.artists = artists.filter(function (artist) %7B%0A      return trackArtistIds.indexOf(artist._id) %3E -1;%0A    %7D);%0A%0A
   %7D);%0A%0A 

add artist information for tracks