'use strict';

var filters = angular.module('picard.filters', []);
filters.filter('debit', function () {
    return function (input) {
        if (input == -1) {
            return 'Cr';
        } else if (input == 1) {
            return 'Dr';
        } else {
            return '';
        }
    };
});

filters.filter('posted', function () {
    return function (input) {
        return input === true ? 'Posted' : 'Post';
    };
});

filters.filter('md', function () {
    var converter = new Showdown.converter();
    return function (input) {
        return converter.makeHtml(input || '');
    };
});

filters.filter('save_button', function () {
    return function (input) {
        return (!input) ? 'Save' : 'Update';
    };
});

filters.filter('journalDebit', function () {
    return function (input, debit) {
        var list = [];
        if (typeof input !== 'undefined') {
            for (var i = 0, l = input.length; i < l; i++) {
                if (input[i].Debit === debit) {
                    list.push(input[i]);
                }
            }
        }
        return list;
    };
});

filters.filter('localTime', function () {
    var months = {
        Jan: 1,
        Feb: 2,
        Mar: 3,
        Apr: 4,
        May: 5,
        Jun: 6,
        Jul: 7,
        Aug: 8,
        Sep: 9,
        Oct: 10,
        Nov: 11,
        Dec: 12
    };
    var months_reverse = {
        1: 'Jan',
        2: 'Feb',
        3: 'Mar',
        4: 'Apr',
        5: 'May',
        6: 'Jun',
        7: 'Jul',
        8: 'Aug',
        9: 'Sep',
        10: 'Oct',
        11: 'Nov',
        12: 'Dec'
    };

    function padNumber(input, width) {
        input = input + '';
        width = width || 2;
        return input.length >= width ? input : new Array(width - input.length + 1).join('0') + input;
    }

    return function (input) {
        if (typeof input === 'undefined') {
            return '';
        }
        var d = input.match(/(\w+)/g);
        if (d.length !== 5) {
            return input;
        }
        var date = new Date(parseInt(d[2]), months[d[1]] - 1, parseInt(d[0]), parseInt(d[3]), parseInt(d[4])).getTime();
        date = new Date(date - (new Date().getTimezoneOffset() * 60000));
        var output = padNumber(date.getDate()) + '-' + months_reverse[date.getMonth() + 1] + '-' + date.getFullYear() + ' ' + padNumber(date.getHours()) + ':' + padNumber(date.getMinutes());
        return output;
    };
});

filters.filter('percent', ['numberFilter', function (numberFilter) {
    return function (input) {
        if (!angular.isNumber(input)) {
            return input;
        }
        return numberFilter(input * 100, 2) + '%';
    };
}]);

filters.filter('clr', function () {
    return function (input) {
        if (input === '\u20B9\u00A00.00' || input === '0.00') {
            return '';
        }
        return input;
    };
});

filters.filter('accounting', ['currencyFilter', function (currencyFilter) {
    return function (amount) {
        amount = currencyFilter(amount);
        if (amount === '') {
            return '';
        }
        if (amount.indexOf('-') !== -1) {
            return amount.replace('-', '') + '\u00A0Cr';
        } else {
            return amount + '\u00A0Dr';
        }
    };
}]);

filters.filter('iif', function () {
   return function(input, trueValue, falseValue) {
        return input ? trueValue : falseValue;
   };
});