var util = require('util'),
    events = require('events'),
    child_process = require('child_process');

var Encoder = exports.Encoder = function(){
    events.EventEmitter.call(this);
    
    
}
util.inherits(Encoder, events.EventEmitter);

Encoder.prototype.default_options = {
    foreground_color: '#000000',
    background_color: '#FFFFFF',
    dot_size: 3, // default 3x3px per dot
    margin: 4, // default 4 dots for the margin
    level: 'L', // valid args (lowest to highest): L, M, Q, H
    case_sensitive: true, // case sensitive,
    version: 1 // default version 1
};

/**
 * Converts a string value to QR Code PNG data, and optionally saves to a file
 *
 * @param String value      The value to be encoded
 * @param String path       Where to save the PNG file (optional)
 * @param Object options    A hash of options (optional) 
 * @return void      
 */
Encoder.prototype.encode = function(value, path, options)
{
    // preserve scope in callbacks with self
    var self = this,
        cmd_options, qrencode_args, stdout, stderr, qrencode, exitcode;
    
    try {
        // check for undefined value
        if(value == undefined) {
            throw new Error('No value specified for encode method');
        }
        
        // create new buffer for value
        value = new Buffer(value);
        
        // if options are given, override defaults
        cmd_options = {};
        if(options == null) options = {};
        for(var key in this.default_options) {
            cmd_options[key] = (options[key] == undefined) ? 
                this.default_options[key] 
                : options[key];
        }
        
        // remove # symbol from color codes because qrencoder does not like it
        cmd_options.foreground_color = cmd_options.foreground_color.replace('#', '');
        cmd_options.background_color = cmd_options.background_color.replace('#', '');
        
        // start with base set of args that we'll always pass
        qrencode_args = [
            '-s', cmd_options.dot_size, 
            '-m', cmd_options.margin,
            '-l', cmd_options.level,
            '-v', cmd_options.version,
            '--foreground=' + cmd_options.foreground_color,
            '--background=' + cmd_options.background_color
        ];
        
        // if case-sensitivity is disabled, add flag
        if(!cmd_options.case_sensitive) qrencode_args.push('-i');
        
        // if we have a path, write to the path
        // otherwise, it will write to stdout
        qrencode_args.push('-o');
        if(path != null) {
            qrencode_args.push(path);
        } else {
            qrencode_args.push('-');
        }
        
        // add the value to be encoded
        qrencode_args.push(value);
        
        // spawn the child process
        qrencode = child_process.spawn(
            'qrencode', 
            qrencode_args
        );
        
        // add event listener for stdout data and populate stdout var
        // in the event no path was given
        qrencode.stdout.on('data', function(data) {
            stdout = data;
        });
        
        // add event listener for stderr
        qrencode.stderr.on('data', function(data) {
            stderr = data;
        });
        
        // add listener for process exit and save exit code
        qrencode.on('exit', function(code) {
            exitcode = code;
        });

        // add listener for filehandle close and emit end or error event
        // depending on exit code
        qrencode.on('close', function() {
            if(exitcode !== 0) {
                self.emit('error', new Error(stderr));
            } else {
                self.emit('end', stdout);
            }
        });
        
    } catch(err) {
        this.emit('error', err);
    }
}

@@ -312,41 +312,135 @@
 00',
-%0A    background_color: '#FFFFFF',
+ // change foreground color in qrencode v3.4.0+%0A    background_color: '#FFFFFF', // change background color in qrencode v3.4.0+
 %0A   
@@ -1752,75 +1752,402 @@
  // 
-remove # symbol from color codes because qrencoder does not like it
+start with base set of args that we'll always pass%0A        qrencode_args = %5B%0A            '-s', cmd_options.dot_size, %0A            '-m', cmd_options.margin,%0A            '-l', cmd_options.level,%0A            '-v', cmd_options.version%0A        %5D;%0A        %0A        // only set foreground and background colors if they differ from%0A        // defaults to maintain compatibility with qrencode pre-v3.4.0
 %0A   
@@ -2143,32 +2143,35 @@
 -v3.4.0%0A        
+if(
 cmd_options.fore
@@ -2175,37 +2175,48 @@
 oreground_color 
-= cmd
+!== self.default
 _options.foregro
@@ -2228,34 +2228,21 @@
 olor
-.replace('#', '');%0A
+%0A  
        
+ %7C%7C
  cmd
@@ -2271,13 +2271,24 @@
 lor 
-= cmd
+!== self.default
 _opt
@@ -2312,27 +2312,14 @@
 olor
-.replace('#', '');%0A
+) %7B%0A  
     
@@ -2335,128 +2335,168 @@
     
+  
 // 
-start with base set of args that we'll always pass%0A        qrencode_args = %5B%0A            '-s', cmd_options.dot_size, 
+remove # symbol from color codes because qrencoder does not like it%0A          cmd_options.foreground_color = cmd_options.foreground_color.replace('#', '');
 %0A   
@@ -2497,32 +2497,24 @@
 );%0A         
-   '-m',
  cmd_options
@@ -2518,91 +2518,114 @@
 ons.
-margin,%0A            '-l', cmd_options.level,%0A            '-v', cmd_options.version,
+background_color = cmd_options.background_color.replace('#', '');%0A          %0A          qrencode_args.push(
 %0A   
@@ -2740,34 +2740,46 @@
 d_color%0A        
-%5D;
+  );%0A        %7D
 %0A        %0A      

maintain compatibility with qrencode pre-v3.4.0