(function () {
    'use strict';
    angular.module('uniflash').config(['$provide', function ($provide) {
            $provide.decorator('$log', ['$delegate', 'ufLogger', function ($delegate, ufLogger) {
                    function supplant(str, o) {
                        return str.replace(/{([^{}]*)}/g, function (a, b) {
                            var r = o[b];
                            return typeof r === 'string' || typeof r === 'number' ? r : a;
                        });
                    }
                    ;
                    function enhancedLog(funcName, origFunc) {
                        return function () {
                            var date = new Date();
                            var hh = ('00' + date.getHours()).slice(-2);
                            var mm = ('00' + date.getMinutes()).slice(-2);
                            var ss = ('00' + date.getSeconds()).slice(-2);
                            var mss = ('000' + date.getMilliseconds()).slice(-3);
                            var now = hh + ':' + mm + ':' + ss + ':' + mss;
                            var args = [].slice.call(arguments);
                            if (!args[0] || args[0] === '') {
                                return;
                            }
                            args[0] = (typeof args[0] === 'string') ? args[0] : JSON.stringify(args[0]);
                            args[0] = supplant('{0} {1} - {2}', [now, funcName, args[0]]);
                            // Call the original with the output prepended with formatted timestamp
                            if (ufLogger.printToConsole) {
                                origFunc.apply(null, args);
                            }
                            ufLogger.log(args[0]);
                        };
                    }
                    ;
                    // augment $log functions
                    var logFn = $delegate.log;
                    $delegate.log = enhancedLog('log', logFn);
                    var infoFn = $delegate.info;
                    $delegate.info = enhancedLog('info', infoFn);
                    var warnFn = $delegate.warn;
                    $delegate.warn = enhancedLog('warn', warnFn);
                    var errorFn = $delegate.error;
                    $delegate.error = enhancedLog('error', errorFn);
                    var debugFn = $delegate.debug;
                    $delegate.debug = enhancedLog('debug', debugFn);
                    return $delegate;
                }]);
        }]);
})();
