'use strict';

var fs = require('fs');
var path = require('path');
var unzip = require('unzip');
var fstream = require('fstream');
var JSONStream = require('json-stream');

var stream = JSONStream();

var main = __filename && process.argv[1] &&
  path.basename(process.argv[1]) === path.basename(__filename);

var letter = 'a';
var length = 13103;
/**
 * parse jsonlines data into Array of Objects
 *
 * @param {string} data - jsonlines
 * @returns {Array} data - list of data objects
 */
function parse(data) {
  var collection = [];
  stream.on('data', function (chunk) {
    collection.push(chunk);
  });
  stream.write(data);
  return collection;
}

/**
 * Get the json data file path which holds all words data of all words.
 */
function getDataFilePath() {
  return path.join(__dirname, letter + '.json');
}

/**
 * Get the json data zip file path which holds all words data of all words.
 */
function getDataZipPath() {
  return path.join(__dirname, letter + '.json.zip');
}

/**
 * Load the json data file which holds all words data of all words.
 *
 * @param {function} callback - A callback(err, data) function to invoke on err or when data loads.
 * List of Objects with definitions for each word.
 */
function load(callback) {
  var dataFile = getDataFilePath();
  return fs.readFile(dataFile, function(err, data) {
    if(err) callback(err);
    callback(null, parse(data));
  });
}

/**
 * Load the json data file which holds all words data of all words.
 *
 * @returns {Array} data - List of Objects with definitions for each word.
 */
function loadSync() {
  var dataFile = getDataFilePath();
  var data = fs.readFileSync(dataFile);
  return parse(data);
}

/**
 * Unzip all json data files which hold all words data that can be loaded, etc.
 * @returns {Array} files - List of files that were uncompressed.
 */
function uncompress() {
  process.stdout.write('inflating: ' + letter + '.json\r\n');
  var readStream = fs.createReadStream( getDataZipPath() );
  var writeStream = fstream.Writer( __dirname );
  readStream
    .pipe( unzip.Parse() )
    .pipe( writeStream )
}

if (main) {
  uncompress();
} else {
  var pkg = require(path.join(__dirname, 'package.json'));
  module.exports = {
    load: load,
    loadSync: loadSync,
    letter: letter,
    length: length,
    name: pkg.name,
    version: pkg.version
  };
}
