module.exports = submissions;
submissions.usage = "fhc submissions [list]\n" +
              "fhc submissions get <submission-id>"
submissions.doList = doList;
submissions.doGetSubmission = doGetSubmission;

var fs = require('fs');
var util = require('util');
var fhc = require("./fhc");
var fhreq = require("./utils/request");

// Get FeedHenry platform version
function submissions (args, cb) {
  var url = fhreq.getFeedHenryUrl();
  if (args.length === 0) return doList(cb);
  var action = args[0];

  if (action === 'list') return doList(cb);
  if (action === 'get') {
    if (!args[1]) return cb(submissions.usage);
    return doGetSubmission(args[1], cb);
  }

  return cb(submissions.usage);
};

function doList(cb) {
  fhreq.POST(fhreq.getFeedHenryUrl(), "api/v2/forms/submission", function (err, remoteData, raw, res) {
    if (err) return cb(err);
    if (res.statusCode !== 200) return cb('Unexpected response code: ' + res.statusCode + ' - ' + raw);

    remoteData.status = "ok";
    remoteData.statusCode = 200;
    return cb(err, remoteData);
  });
};

function doGetSubmission(submissionId, cb) {
  fhreq.GET(fhreq.getFeedHenryUrl(), "api/v2/forms/submission/" + submissionId, function (err, remoteData, raw, res) {
    if (err) return cb(err);
    if (res.statusCode !== 200) return cb('Unexpected response code: ' + res.statusCode + ' - ' + raw);

    remoteData.status = "ok";
    remoteData.statusCode = 200;
    return cb(err, remoteData);
  });
};



@@ -126,10 +126,88 @@
 -id%3E
+%5Cn%22 + %0A              %22fhc submissions getfile %3Csubmission-id%3E %3Coutputfilename%3E
 %22%0A
-
 subm
@@ -398,16 +398,100 @@
 quest%22);
+%0Avar url = require('url');%0Avar http = require('http');%0Avar https = require('https');
 %0A%0A// Get
@@ -820,16 +820,164 @@
 cb);%0A  %7D
+%0A  if (action === 'getfile') %7B%0A    if (!args%5B1%5D %7C%7C !args%5B2%5D) return cb(submissions.usage);%0A    return doGetSubmissionFile(args%5B1%5D, args%5B2%5D, cb);%0A  %7D
 %0A%0A  retu
@@ -1724,32 +1724,32 @@
 atusCode = 200;%0A
-
     return cb(er
@@ -1774,9 +1774,1108 @@
 %7D);%0A%7D;%0A%0A
+function doGetSubmissionFile(fileGroupId, outputFileName, cb) %7B%0A  //TODO the main part of this function should be moved into utils/request.js%0A  var fhUrl = url.resolve(fhreq.getFeedHenryUrl(),  %22api/v2/forms/submission/file/%22 + fileGroupId);%0A%0A  var uri = url.parse(fhUrl);%0A  var proto = uri.protocol === 'https:' ? https : http;%0A%0A  var headers = %7B%7D;%0A  var cookie = fhc.config.get(%22cookie%22);%0A  if (cookie != undefined) %7B%0A    headers.cookie = %22feedhenry=%22 + cookie + %22;%22;%0A  %7D%0A%0A  uri.headers = headers;%0A%0A  var req = proto.get(uri, function(res)%7B%0A    if(res.statusCode !== 200) return cb(%22Unexpected response code for file download: %22 + res.statusCode + %22 message: %22 + res.body);%0A%0A    var stream = fs.createWriteStream(outputFileName);%0A    res.on('data',function (chunk) %7B%0A      stream.write(chunk);%0A    %7D);%0A%0A    res.on('end',function ()%7B%0A      stream.end(function () %7B%0A        return cb(undefined, %7Bstatus: %22ok%22, statusCode: res.statusCode, url: fhUrl, file: outputFileName%7D);%0A      %7D);%0A    %7D);%0A  %7D);%0A%0A  req.on('error',function(err)%7B%0A     return cb(%22Error downloading file: %22 + err.message);%0A  %7D);%0A%7D;%0A%0A
 %0A

add submissions file download