'use strict';

/*!
 * imager
 * Copyright(c) 2014 Madhusudhan Srinivasa <madhums8@gmail.com>
 * MIT Licensed
 */

/**
 * Module dependencies.
 */

var fs = require('fs');
var gm = require('graphicsmagick-stream');
var mime = require('mime');
var co = require('co');
var pkgcloud = require('pkgcloud');
var debug = require('debug')('imager');

/**
 * Expose
 */

module.exports = Imager;

/**
 * Imager
 *
 * @param {Object} variants
 * @param {Object} storage
 * @api public
 */

function Imager (variants, storage) {
  this.client = pkgcloud.storage.createClient(storage);
  this.variants = variants;
  this.storage = storage;
}

/**
 * upload
 *
 * @param {Array|String} files
 * @@param {String} variant
 * @param {Function} cb
 * @api public
 */

Imager.prototype.upload = function (files, variant, fn) {
  var self = this;

  if (typeof files === 'string') {
    files = [files];
  }

  var presets = self.variants[variant];
  presets = Object.keys(presets).map(function (key, index) {
    presets[key].__name = key;
    return presets[key];
  });

  co(function *() {
    // modified files having the same object structure
    var _files = yield files.map(info);
    var arr = [];

    _files.forEach(function (file) {
      presets.forEach(function (preset) {
        arr.push({ file: file, preset: preset });
      });
    });

    try {
      var uploaded = yield arr.map(upload.bind(self));
      fn(null, uploaded);
    } catch (err) {
      fn(err);
    }
  })();
};

/**
 * upload
 *
 * @param {Object} file
 * @api private
 */

function upload (obj) {
  var storage = this.storage;
  var client = this.client;
  var preset = obj.preset;
  var file = obj.file;
  var filename = preset.rename && preset.rename(file, preset.__name);

  return function (fn) {
    var _convert = gm(preset);
    var _upload = client.upload({
      container: storage.container,
      remote: filename || (preset.__name + '_' + file.name)
    }, done);

    function done (err, uploaded, res) {
      if (err) return fn(err);
      // Pass the uploaded image url
      fn(err, res.request.href);
    }

    // Convert and upload
    fs.createReadStream(file.path)
      .pipe(_convert())
      .pipe(_upload);
  };
}

/**
 * info
 *
 * @param {Object} file
 * @return {Object}
 * @api private
 */

function *info (file) {
  return {
    size: typeof(file) === 'string'
      ? (yield stat(file)).size
      : file.size,
    type: typeof(file) === 'string'
      ? mime.lookup(file)
      : file.type || file.mimetype || file.headers['content-type'],
    name: typeof(file) === 'string'
      ? file.split('/')[file.split('/').length - 1]
      : file.name,
    path: typeof(file) === 'string'
      ? file
      : file.path
  };
}

/**
 * stat
 *
 * @param {String} path
 * @return {Function}
 * @api private
 */

function stat (path) {
  return function (done) {
    fs.stat(path, done);
  }
}

@@ -602,23 +602,25 @@
 %0A  this.
-storage
+container
  = stora
@@ -621,16 +621,26 @@
  storage
+.container
 ;%0A%7D%0A%0A/**
@@ -1581,30 +1581,34 @@
 var 
-storage = this.storage
+container = this.container
 ;%0A  
@@ -1861,24 +1861,16 @@
 tainer: 
-storage.
 containe
@@ -2031,17 +2031,8 @@
   //
- Pass the
  upl
@@ -2094,17 +2094,17 @@
 %0A    // 
-C
+c
 onvert a

maintain container name in an instance variable