(function () {
    global.dashModule.service('finanncingService', [finanncingService]);

    function finanncingService() {
        return {
            calculateFinancing
        };

        /**
         * Copy of Excel's PMT function.
         * Credit: http://stackoverflow.com/questions/2094967/excel-pmt-function-in-js
         *
         * @param ratePerPeriod       The interest rate for the loan.
         * @param numberOfPayments    The total number of payments for the loan in months.
         * @param presentValue         The present value, or the total amount that a series of future payments is worth now;
         *                              Also known as the principal.
         * @param futureValue          The future value, or a cash balance you want to attain after the last payment is made.
         *                              If fv is omitted, it is assumed to be 0 (zero), that is, the future value of a loan is 0.
         * @param type                  Optional, defaults to 0. The number 0 (zero) or 1 and indicates when payments are due.
         *                              0 = At the end of period
         *                              1 = At the beginning of the period
         * @returns {number}
         */
        function calculateFinancing(ratePerPeriod, numberOfPayments, presentValue, futureValue = 0, type = 0) {
            /*var q = 0;
            var c = 0;
            const monthlyRatePerPeriod = ratePerPeriod / 12;

            if (monthlyRatePerPeriod !== 0.0) {
                // Interest rate exists
                q = Math.pow(1 + monthlyRatePerPeriod, numberOfPayments);
                c = (monthlyRatePerPeriod * (futureValue + (q * presentValue))) / ((-1 + q) * (1 + monthlyRatePerPeriod * (type)));
                return c.toFixed(2);

            } else if (numberOfPayments !== 0.0) {
                // No interest rate, but number of payments exists
                return -(futureValue + presentValue) / numberOfPayments;
            }

            return 0;*/
            const rates = {
                24 : 4.282,
                30 : 3.451,
                36 : 2.896,
                42 : 2.500,
                48 : 2.223,
                54 : 2.025,
                60 : 1.834
            };

            const interest = rates[numberOfPayments] || 10;

            return presentValue * (interest / 100);
        }
    }
})();
