"use strict";

var events = require('events');
var extend = require('obj-extend');
var magick = require('magick');
var url = require('url');

var ImagineMagick = function (ops, key) {
  this.ops = ops;
  this.key = key;

  this.data = [];
  this.dataLength = 0;
};

ImagineMagick.operations = {

  'resize\/([0-9]+?)x([0-9]+?)(\/|$)': function (file, width, height) {
    file.resize(parseInt(width, 10), parseInt(height, 10));
  },
  'crop\/([0-9]+?)x([0-9]+?)(\/|$)': function (file, width, height) {
    file.crop(parseInt(width, 10), parseInt(height, 10));
  },
  'resize_to_fill\/([0-9]+?)x([0-9]+?)(\/|$)': function (file, width, height) {
    file.resizeToFill(parseInt(width, 10), parseInt(height, 10));
  },
  'opacity\/([0-9]+?)(\/|$)': function (file, opacity) {
    file.opacity(parseInt(opacity, 10) / 100);
  },
  'blur\/([0-9]+?)(\/|$)': function (file, sigma) {
    sigma = !sigma || isNaN(sigma) ? 0 : parseInt(sigma, 10);
    file.blur(sigma);
  },
  'charcoal\/([0-9]+?)(\/|$)': function (file, sigma) {
    file.charcoal(parseInt(sigma, 10));
  },
  'paint\/([0-9]+?)(\/|$)': function (file, radius) {
    file.paint(parseInt(radius, 10));
  },
  'sepia\/([0-9]+?)(\/|$)': function (file, threshold) {
    file.threshold(parseInt(threshold, 10));
  }

};

ImagineMagick.prototype = extend({}, events.EventEmitter.prototype, {

  handleResponse: function (response) {
    if (this.ops && this.ops.length > 1) {
      this.response = response;

      this.proxy = new events.EventEmitter();
      this.proxy.headers = this.response.headers;
      delete this.proxy.headers['content-length'];

      this.response.on('data', this.collectData.bind(this));
      this.response.on('end', this.onEnd.bind(this));
    } else {
      this.emit('response', response);
    }
  },

  handleAbort: function () {
    if (this.response) {
      this.response.removeAllListeners();
    }
  },

  collectData: function (chunk) {
    this.data.push(chunk);
    this.dataLength += chunk.length;
  },

  getBuffer: function () {
    if (!this.buffer) {
      this.buffer = new Buffer(this.dataLength);
      var position = 0;
      this.data.forEach(function (chunk) {
        chunk.copy(this.buffer, position);
        position = chunk.length;
      }, this);
      this.data = [];
      this.dataLength = 0;
    }
    return this.buffer;
  },

  onEnd: function () {
    var file = new magick.File(this.getBuffer());
    this.performOperations(file);

    this.emit('response', this.proxy);
    this.proxy.emit('data', file.getBuffer());
    this.proxy.emit('end');

    file.release();
    file = null;
    this.buffer = null;
  },

  performOperations: function (file) {
    var operations = ImagineMagick.operations;
    var operation, match;
    for (operation in operations) {
      if (operations.hasOwnProperty(operation)) {
        match = this.ops.match(new RegExp(operation));
        if (match) {
          match.splice(0, 1, file);
          operations[operation].apply(this, match);
        }
      }
    }
  }

});

module.exports = ImagineMagick;

@@ -216,50 +216,8 @@
 key;
-%0A%0A  this.data = %5B%5D;%0A  this.dataLength = 0;
 %0A%7D;%0A
@@ -1891,110 +1891,12 @@
     
-this.data.push(chunk);%0A    this.dataLength += chunk.length;%0A  %7D,%0A%0A  getBuffer: function () %7B%0A    if (!
+if (
 this
@@ -1908,29 +1908,28 @@
 er) %7B%0A      
-this.
+var 
 buffer = new
@@ -1945,90 +1945,97 @@
 his.
-dataLength);%0A      var position = 0;%0A      this.data.forEach(function (chunk) %7B%0A  
+buffer.length + chunk.length);%0A      this.buffer.copy(buffer, 0, 0, this.buffer.length);%0A
     
@@ -2047,16 +2047,24 @@
 nk.copy(
+buffer, 
 this.buf
@@ -2070,148 +2070,108 @@
 ffer
-, position);%0A        position = chunk.length;%0A      %7D, this);%0A      this.data = %5B%5D;%0A      this.dataLength = 0;%0A    %7D%0A    return this.buffer;
+.length, 0, chunk.length);%0A      this.buffer = buffer;%0A    %7D else %7B%0A      this.buffer = chunk;%0A    %7D
 %0A  %7D
@@ -2232,27 +2232,22 @@
 le(this.
-getB
+b
 uffer
-()
 );%0A    t

Change buffering operation