'use strict';

var mod = angular.module('bananaraceApp.filters', []);

// mod.filter('mstotext', ['milliseconds', function(milliseconds) {

//   function msToTextFilter(input) {
//     return 'filtered'+input;
//   }

//   return msToTextFilter;
// }]);

mod.filter('secondsToString', function() {
    return function(seconds) {
        var minutes = Math.floor(seconds / 60);
        var secondsleft = seconds - (minutes * 60);

        var padWithZeroes = function(input, desiredlength) {
            var str = input + "";
            while (str.length < desiredlength) {
                str = "0" + str;
            }
            return str;
        };

        var seconds = Math.floor(secondsleft);
        var decimals = secondsleft - seconds;

        return padWithZeroes(minutes, 2) + ':' + padWithZeroes(seconds, 2) + "." + padWithZeroes(Math.floor(decimals * 10), 1);
    };
});

mod.filter('interval', function() {
    return function(input) {
        if (!input) {
            return undefined;
        }
        var parts = input.split('.');
        var ms = 0;
        if (parts.length == 2) {
            // microseconds available
            ms = parts[1];
            ms /= Math.pow(10, (ms.length - 1));
            ms = Math.round(ms);
            ms = ms == 10 ? 1 : ms;
        }
        var pieces = parts[0].split(':');
        var hours = pieces[0];
        var mins = pieces[1];
        var secs = pieces[2];

        var totalMins = hours * 60 + parseInt(mins);
        totalMins = totalMins < 10 ? '0' + totalMins : totalMins;
        return totalMins + ':' + secs + '.' + ms;
    }
});