var Calculators = Calculators || {};
Calculators.loan = Calculators.loan || {};

Calculators.loan.AutoLoanPayment = function AutoLoanPayment(payment, totalPrice, monthsOfPayments, annualInterestRate, downPayment, tradeInAmount, amountOwedOnTradeIn) {
	var formula = new Calculators.math.Formulas();
	this.payment = new Calculators.type.Money(payment);
	this.totalPrice = new Calculators.type.Money(totalPrice);
	this.annualInterestRate = new Calculators.type.Rate(annualInterestRate);
	this.downPayment = new Calculators.type.Money(downPayment);
	this.monthsOfPayments = new Number(monthsOfPayments);
	this.tradeInAmount = new Calculators.type.Money(tradeInAmount);
	this.amountOwedOnTradeIn = new Calculators.type.Money(amountOwedOnTradeIn);
	this.statements = [];
	var that = this;
	
	this.getPayment = function() {
		if(this.payment.getAmount() == 0.0) {
			return computePayment()
		} else {
			return this.payment;
		}
	}
	
	this.getTotalPrice = function() {
		if(this.totalPrice.getAmount() == 0.0) {	
			return computeTotalPrice();
		} else {
			return this.totalPrice;
		}
	}
	
	this.getAnnualInterestRate = function() {
		return this.annualInterestRate();
	}
	
	this.getDownPayment = function() {
		return this.downPayment;
	}
	
	this.getMonthsOfPayments = function() {
		return this.monthsOfPayments;
	}
	
	this.getTradeInAmount = function() {
		return this.tradeInAmount;
	}
	
	this.getAmountOwedOnTradeIn = function() {
		return this.amountOwedOnTradeIn;
	}
	
	this.getLoanAmount = function() {
		var loanAmount = this.getTotalPrice().getAmount() - this.downPayment.getAmount() - this.tradeInAmount.getAmount() + this.amountOwedOnTradeIn.getAmount();
		return new Calculators.type.Money(loanAmount);
	}
	
	this.getTotalInterest = function() {
		var interest = (this.getPayment().getAmount() * this.monthsOfPayments) - this.getLoanAmount().getAmount();
		return new Calculators.type.Money(interest);
	}
	
	function computePayment() {
		var	payment = formula.pmt(that.annualInterestRate.monthly(), that.monthsOfPayments, (-1 * (that.totalPrice.getAmount() - that.downPayment.getAmount() - that.tradeInAmount.getAmount() + that.amountOwedOnTradeIn.getAmount())), 0);
		
		return new Calculators.type.Money(payment);
	}
	
	function computeTotalPrice() {
		var totalPrice = formula.pv(that.annualInterestRate.monthly(), that.monthsOfPayments, that.payment.getAmount(), 0);
		totalPrice += (that.downPayment.getAmount() + that.tradeInAmount.getAmount() - that.amountOwedOnTradeIn.getAmount())
		return new Calculators.type.Money(Math.round(totalPrice));
	}
	
	this.getStatements = function() {
		return this.statements;
	}
	
	this.calculate = function() {
		var pmt = this.getPayment();
		this.statements[0] = new Calculators.loan.Statement(this.getLoanAmount(), 0.00, 0.00);
		this.statements[0].payment = pmt;
		for (var i=1; i<this.monthsOfPayments; i++) {
			var lastBalance = this.statements[i-1].getBalance().getAmount();			
			var interest = (lastBalance * this.annualInterestRate.monthly());
			var principal = (pmt.getAmount() - interest);
			var balance = (lastBalance - principal);
			var stmt = new Calculators.loan.Statement(balance, interest, principal);
			stmt.payment = pmt;
			this.statements[i] = stmt;
		}
		var secondToLastStatement = this.statements[this.statements.length - 1];
		var interest = (secondToLastStatement.getBalance().getAmount() * this.annualInterestRate.monthly());
		var balance = 0;
		var principal = secondToLastStatement.getBalance().getAmount();
		var stmt = new Calculators.loan.Statement(balance, interest, principal);
		stmt.payment = new Calculators.type.Money(interest + principal);
		this.statements[this.statements.length] = stmt;
		//console.debug(this.statements);
	}
	
	this.calculate();
}