// Copyright 2014, Renasar Technologies Inc.
/* jshint: node:true */

'use strict';

var di = require('di');

module.exports = loggingProtocolFactory;

di.annotate(loggingProtocolFactory, new di.Provide('Protocol.Logging'));
di.annotate(loggingProtocolFactory,
    new di.Inject(
        'Assert',
        'Constants',
        'Services.Messenger',
        '_',
        'Result',
        'Q'
    )
);

function loggingProtocolFactory (assert, Constants, messenger, _, Result, Q) {
    var levels = _.keys(Constants.Logging.Levels);

    function LoggingProtocol() {
    }

    /**
     * Sends a log message out over the messenger...
     *
     * @param level {string} one of ['emerg','alert','crit','error','warning',
     *                       'notice','info','debug','silly']
     * @param data {Object}
     * @returns {Q.promise} resolving if everything worked, rejecting with
     *                      relevant error if it didn't
     */
    LoggingProtocol.prototype.publishLog = function (level, data) {
        assert.isIn(level, levels);
        assert.object(data, 'data');

        return messenger.publish(
            Constants.Protocol.Exchanges.Logging.Name,
            level,
            new Result({ value: data })
        );
    };

    /**
     * Subscribes to the log feed for the given message level
     *
     * @param level {string} one of ['emerg','alert','crit','error','warning',
     *                       'notice','info','debug','silly']
     * @param callback {function} callback taking a single value - the message
     * @returns {Q.promise} resolving if everything worked, rejecting with
     *                      relevant error if it didn't
     */
    LoggingProtocol.prototype.subscribeLog = function(level, callback) {
        assert.isIn(level, levels);
        assert.func(callback);

        return messenger.subscribe(
            Constants.Protocol.Exchanges.Logging.Name,
            level,
            function(data, message) {
                Q.resolve().then(function() {
                    return callback(data.value);
                }).then(function (result) {
                    message.resolve(
                        new Result({ value: result })
                    );
                }).catch(function (error) {
                    message.reject(error);
                });
            }
        );
    };


    return new LoggingProtocol();
}

@@ -1207,36 +1207,13 @@
     
- new Result(%7B value:
  data
- %7D)
 %0A   
@@ -1931,407 +1931,16 @@
     
-function(data, message) %7B%0A                Q.resolve().then(function() %7B%0A                    return callback(data.value);%0A                %7D).then(function (result) %7B%0A                    message.resolve(%0A                        new Result(%7B value: result %7D)%0A                    );%0A                %7D).catch(function (error) %7B%0A                    message.reject(error);%0A                %7D);%0A            %7D
+callback
 %0A   

fix message protocol object type for marshaling #138