/* jshint node: true */
'use strict';

var AWS = require('aws-sdk');
var Promise = require('bluebird');

var config = require('../config/config');

var s3 = new AWS.S3();

function listObject(params) {
    return new Promise(function (resolve, reject) {
        s3.listObjects(params, function(err, data) {
          if (err) {
              reject(err);
          } else {
              resolve(data.Contents);
          }
        });
    });
}

function itemDownload(params) {
    return new Promise(function (resolve, reject) {
        s3.getObject(params, function (err, data) {
            if (err) {
                reject(err);
            } else {
                console.log(data.Body);
                resolve(data.Body);
            }
        });
    });
}

function listItem(results) {
    return results.map(function (item) {
        itemDownload({
            Bucket: config.s3.Bucket,
            Key: item.Key
        });
    });
}

module.exports = {
    listObject: listObject,
    itemDownload: itemDownload,
    listItem: listItem
};
