/*jshint esversion:6, node:true*/
'use strict';

var EventEmitter = require('events');

class Connection extends EventEmitter {
    constructor(socket){
        super();

        this.socket = socket;
        this.socket.columns = Connection.DEFAULTS.columns;

        this.init(Connection.DEFAULTS);
    }

    init(options){
        if(this.initialized) return;
        this.initialized = true;
        
        var config = options.makeConfig(this.socket);

        this.repl = options.startConnection(config);
        this.repl.pause();

        this.repl.on('exit', this.onExit.bind(this));

        this.repl.on('error', (e) => {
            this.emit('error', e);
        });

        this.socket.on('close', this.onClose.bind(this));
    }

    start(prompt){
        this.repl.setPrompt(prompt);
        this.repl.resume();
        this.repl.write('', {name: 'return'});
    }

    onExit(){
        this.repl.removeAllListeners();
        this.socket.end();
        this.emit('exit');
    }

    onClose(){
        this.socket.removeAllListeners();
        this.socket.destroy();
        this.socket = null;
        this.repl = null;
    }

    setContext(context){
        Object.keys(context).map((key)=>{
            this.repl.context[key] = context[key];
        });
    }
}

Connection.DEFAULTS = {
    startConnection: function(config){
        return require('repl').start(config);
    },
    colums: 132,
    makeConfig: (socket) => {
        return {
            prompt: '',
            input: socket,
            output: socket,
            terminal: true,
            useGlobal: false,
            useColors: true,
            ignoreUndefined: true,
            replMode: require('repl').REPL_MODE_STRICT
        }
    }
};

module.exports = Connection;

@@ -1,38 +1,4 @@
-/*jshint esversion:6, node:true*/%0A
 'use
@@ -8,19 +8,21 @@
 rict';%0A%0A
-var
+const
  EventEm
@@ -48,16 +48,73 @@
 ents');%0A
+const _getGuid = require('./utils').getGuid;%0A%0A/**%0A * %0A */
 %0Aclass C
@@ -146,16 +146,17 @@
 itter %7B%0A
+%0A
     cons
@@ -170,16 +170,17 @@
 (socket)
+ 
 %7B%0A      
@@ -345,16 +345,17 @@
 options)
+ 
 %7B%0A      
@@ -358,16 +358,17 @@
       if
+ 
 (this.in
@@ -419,24 +419,16 @@
 = true;%0A
-        
 %0A       
@@ -432,11 +432,13 @@
     
-var
+const
  con
@@ -473,24 +473,55 @@
 s.socket);%0A%0A
+        this.id = _getGuid();%0A%0A
         this
@@ -792,24 +792,184 @@
 s));%0A    %7D%0A%0A
+    /**%0A     * We start a new REPL client, and we send%0A     * a prompt we display to our users.%0A     * %0A     * @param %7BString%7D prompt Promt to be shown%0A     */%0A
     start(pr
@@ -970,16 +970,192 @@
 art(
+%7B 
 prompt
-)%7B
+, dirtyPrompt %7D) %7B%0A        /*%0A         * Store our prompts.%0A         */%0A        this.prompt = prompt;%0A        this.dirtyPrompt = dirtyPrompt;%0A%0A        this.addClientCommands();
 %0A   
@@ -1245,16 +1245,17 @@
 te('', %7B
+ 
 name: 'r
@@ -1260,16 +1260,460 @@
 'return'
+ %7D);%0A    %7D%0A%0A    /**%0A     * Add REPL commands. This commands will %0A     * be available under the dot commands%0A     * alongside native commands like:%0A     * - .help%0A     * %0A     */%0A    addClientCommands() %7B%0A        const conn = this;%0A%0A        this.repl.defineCommand('clean', %7B%0A            help: 'Acknowledge dirty prompt',%0A            action() %7B%0A                conn.setCleanPrompt();%0A                this.displayPrompt();%0A            %7D%0A        
 %7D);%0A    
@@ -1727,16 +1727,17 @@
 onExit()
+ 
 %7B%0A      
@@ -1844,16 +1844,17 @@
 nClose()
+ 
 %7B%0A      
@@ -2004,16 +2004,17 @@
 context)
+ 
 %7B%0A      
@@ -2045,18 +2045,20 @@
 ap((key)
+ 
 =%3E
+ 
 %7B%0A      
@@ -2119,16 +2119,357 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Set connection prompt to%0A     * reflect dirty state. %0A     * This will happen when we update the%0A     * context.%0A     */%0A    setDirtyPrompt() %7B%0A        this.repl.setPrompt(this.dirtyPrompt);%0A    %7D%0A%0A    /**%0A     * Restore prompt to default value.%0A     */%0A    setCleanPrompt() %7B%0A        this.repl.setPrompt(this.prompt);%0A    %7D
 %0A%7D%0A%0AConn
@@ -2517,24 +2517,25 @@
 function
+ 
 (config)
 %7B%0A      
@@ -2526,16 +2526,17 @@
 (config)
+ 
 %7B%0A      

Add guid to connections. Manage prompts.