'use strict';

/**
 * Message module used for broadcast message information accross the application.
 * Including:
 *   Message
 */

angular.module('system.services.message', [
    'config.services'
])
    .service('Message', [
        '$rootScope',
        'Config',
        function($rootScope, Config) {
            var info = function(value) {
                message(Config.SYSTEM_MESSAGE, { type: Config.SYSTEM_INFO_KEY, value: value });
            };

            var warning = function(value) {
                message(Config.SYSTEM_MESSAGE, { type: Config.SYSTEM_WARNING_KEY, value: value });
            };

            var error = function(value) {
                message(Config.SYSTEM_MESSAGE, { type: Config.SYSTEM_ERROR_KEY, value: value });
            };

            var success = function(value) {
                message(Config.SYSTEM_MESSAGE, { type: Config.SYSTEM_SUCCESS_KEY, value: value });
            };

            var message = function (key, value) {
                $rootScope.$broadcast(key, value);
            };
            
            var on = function (scope, fn) {
                scope.$on(Config.SYSTEM_MESSAGE, fn);
            };

            return {
                info: info,
                warning: warning,
                success: success,
                error: error,
                message: message,
                on: on
            };
        }
    ]);