var command_state = require('./protocol-state.js'),
    nconf = require('nconf');
nconf.env()
    .file({"file": "./lib/epp-config.json"});
var moment= require('moment');

var state;

module.exports = function() {

    return {
        "processMessage": function processMessage(m) {

            if (state) {
                if (m.command) {
                    var command = m.command;
                    var data = m.data;
                    var transactionId = data.transactionId;
                    if (! transactionId) {
                        transactionId = ['iwmn', moment().unix()].join('-');
                    }
                    state.command(command, data, transactionId, function(responseData) {
                        process.send(responseData);
                    });
                }
            }
            else { // Initialise a connection instance with registry configuration.
                var registry = m.registry;
                var registryConfig = nconf.get(registry);
                state = command_state(registry, registryConfig);
                var loginTransactionId = ['iwmn', 'login', moment().unix()].join('-');

                // Initialise the connection stream. Upon connection, attempt
                // to login.
                state.connection.initStream(function() {
                    console.log("Got connection");
                    setTimeout(function() {
                        state.command('login', {
                            "login": registryConfig.login,
                            "password": registryConfig.password
                        }, loginTransactionId,
                        function(data) {
                            console.log("Log in callback received: ", data);
                        });
                    }, 2000);
                });
            }
        }
    };
};


var command_state = require('./protocol-state.js'),
nconf = require('nconf');
nconf.env().file({
    "file": "./lib/epp-config.json"
});
var moment = require('moment');

var state;

function Protocol() {

}

Protocol.prototype.processMessage = function processMessage(m) {

    if (state) {
        if (m.command) {
            var command = m.command;
            var data = m.data;
            var transactionId = data.transactionId;
            if (!transactionId) {
                transactionId = ['iwmn', moment().unix()].join('-');
            }
            state.command(command, data, transactionId, function(responseData) {
                process.send(responseData);
            });
        }
    }
    else { // Initialise a connection instance with registry configuration.
        var registry = m.registry;
        var registryConfig = nconf.get(registry);
        state = command_state(registry, registryConfig);
        var loginTransactionId = ['iwmn', 'login', moment().unix()].join('-');

        // Initialise the connection stream. Upon connection, attempt
        // to login.
        state.connection.initStream(function() {
            console.log("Got connection");
            setTimeout(function() {
                state.command('login', {
                    "login": registryConfig.login,
                    "password": registryConfig.password
                },
                loginTransactionId, function(data) {
                    console.log("Log in callback received: ", data);
                });
            },
            2000);
        });
    }
};
module.exports = Protocol;


change protocol to be more prototyped