'use strict';

/*
 * HTTP Upload Appender for ngLogCustom module
 *
 * Uploads the logs that have a log level >= minLevel to postUrl in the specified interval.
 * No uploads happen if no suitable logs have been produced.
 */
function CatHttpLogAppender($http,
                            $interval,
                            $log,
                            config,
                            HTTP_LOGGER_NAME,
                            LOG_LEVEL_ORDER) {
    var logger = $log.Logger(HTTP_LOGGER_NAME);
    if (typeof config.postUrl === 'undefined') {
        throw new Error('catHttpLogAppenderProvider requires definition of postUrl');
    }
    var logs = [];

    this.report = function (level, group, message, memorySizes) {
        logs.push({
            level: level,
            group: group,
            message: typeof message === 'string' ? message : message.toString(),
            memorySizes: memorySizes,
            timestamp: new Date().getTime()
        });
    };
    this.flush = function () {
        var minLevelOrder = LOG_LEVEL_ORDER[config.minLevel];
        var logsToSend = [];
        angular.forEach(logs, function (logEntry) {
            if (LOG_LEVEL_ORDER[logEntry.level] >= minLevelOrder) {
                logsToSend.push(logEntry);
            }
        });
        logs.length = 0;
        if (logsToSend.length > 0) {
            return $http.post(config.postUrl, logsToSend)
                .success(function () {
                    logger.debug('Successfully uploaded logs.');
                })
                .error(function (data, status, headers, config, statusText) {
                    logger.debug('Error uploading logs: ' + status + ' ' + statusText);
                });
        } else {
            logger.debug('No logs to upload - skipping upload request.');
        }
    };
    $interval(this.flush, config.intervalInSeconds * 1000, 0, false);
}


function CatHttpLogAppenderProvider() {
    var intervalInSeconds = 10;
    var postUrl;
    var minLevel = 'info';
    this.interval = function (_intervalInSeconds) {
        intervalInSeconds = _intervalInSeconds;
        return this;
    };
    this.postUrl = function (_postUrl) {
        postUrl = _postUrl;
        return this;
    };
    this.minUploadLevel = function (_minLevel) {
        minLevel = _minLevel;
        return this;
    };

    this.$get = ['$http', '$interval', '$log', 'HTTP_LOGGER_NAME', 'LOG_LEVEL_ORDER',
        function ($http, $interval, $log, HTTP_LOGGER_NAME, LOG_LEVEL_ORDER) {
            var config = {
                intervalInSeconds: intervalInSeconds,
                postUrl: postUrl,
                minLevel: minLevel
            };
            return new CatHttpLogAppender($http, $interval, $log, config, HTTP_LOGGER_NAME, LOG_LEVEL_ORDER);
        }];
}

angular
    .module('cat.service.log')
    .constant('HTTP_LOGGER_NAME', 'catHttpLogAppender')
    .provider('catHttpLogAppender', [CatHttpLogAppenderProvider]);
@@ -1413,18 +1413,26 @@
 gsToSend
+, config
 )%0A
-
         
@@ -2030,16 +2030,37 @@
 'info';%0A
+    var config = %7B%7D;%0A
     this
@@ -2382,16 +2382,117 @@
 ;%0A    %7D;
+%0A    this.setConfig = function(key, value) %7B%0A        config%5Bkey%5D = value;%0A        return this;%0A    %7D;
 %0A%0A    th
@@ -2666,39 +2666,15 @@
     
-var 
 config
- = %7B%0A                
+.
 inte
@@ -2686,17 +2686,18 @@
 nSeconds
-:
+ =
  interva
@@ -2706,17 +2706,17 @@
 nSeconds
-,
+;
 %0A       
@@ -2724,28 +2724,32 @@
     
-    
+config.
 postUrl
-:
+ =
  postUrl
 ,%0A  
@@ -2744,17 +2744,17 @@
  postUrl
-,
+;
 %0A       
@@ -2758,28 +2758,31 @@
         
-    
+config.
 minLevel
 : minLev
@@ -2773,17 +2773,18 @@
 minLevel
-:
+ =
  minLeve
@@ -2784,30 +2784,16 @@
 minLevel
-%0A            %7D
 ;%0A      
@@ -3006,16 +3006,16 @@
 ender')%0A
-
     .pro
@@ -3072,8 +3072,9 @@
 vider%5D);
+%0A

Make config settable through the provider. Fixes #3