#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdint.h>


void Usage(void){
	printf("./loan [loan_balance] [interest_rate] [monthly_payment] [escrow/insurance] [one time lump]\n");
}
/*
The following formula is used to calculate the fixed monthly payment (P) required to fully amortize a loan of L dollars over a term of n months at a monthly interest rate of c. [If the 
quoted rate is 6%, for example, c is .06/12 or .005]. 
P = L[c(1 + c)n]/[(1 + c)n - 1]

The next formula is used to calculate the remaining loan balance (B) of a fixed payment loan after p months.
B = L[(1 + c)n - (1 + c)p]/[(1 + c)n - 1]

 the amount owed every month equals the amount owed from the previous month plus interest on that amount, minus the fixed monthly payment;
*/
int main(int argc, char **argv){
	double loan_balance = 0;
	double loan_balance_orig = 0;
	double interest_rate = 0;
	double nominal_interest_rate = 0;
	double escrow = 250;
	double escrow_total = 0;

	double monthly_payment = 0;
	double payment = 0;
	double interest = 0;
	double principal = 0;
	double lump = 0;
	//double extra_payment;
	double tmp1, tmp2;
	uint32_t months = 0;
	uint32_t max_months;
	//uint32_t years = 0;
	//uint32_t num1 = 0;

	if(argc != 7){
		Usage();
		exit(1);
	}
	loan_balance = atof(argv[1]);
	interest_rate = atof(argv[2]);
	loan_balance_orig = loan_balance;
	nominal_interest_rate = interest_rate/12/100;
	monthly_payment = atof(argv[3]);
	escrow = atof(argv[4]);
	lump = atof(argv[5]);
	max_months = atof(argv[6]);
//	monthly_payment -= escrow;


	if(lump != 0){
		if(lump < monthly_payment)
			printf("warning: lump is smaller than monthly_payment\n");
		tmp1 = nominal_interest_rate*loan_balance;
		if(escrow+tmp1 > lump){
			printf("warning: lump don't cover escrow+interest\n");
		}
		tmp2 = (lump - escrow - tmp1);
		escrow_total += escrow;
		principal += tmp2;
		interest += tmp1;
		payment += lump;
		loan_balance -= tmp2;
		months++;
	}
	//}else{
	//	printf("warning: lump is smaller than monthly_payment\n");
	//}
/*
	while((loan_balance-monthly_payment) > 0){

		tmp1 = nominal_interest_rate*loan_balance;
		tmp2 = (monthly_payment - tmp1);
		escrow_total += escrow;
		principal += tmp2;
		interest += tmp1;
		payment += monthly_payment;
		loan_balance -= tmp2;
		months++;
	}
*/
	if(loan_balance > 0 && months < max_months){
		tmp1 = nominal_interest_rate*loan_balance;
		while((monthly_payment-tmp1-escrow) < loan_balance){
			principal += monthly_payment-tmp1-escrow;
			payment += monthly_payment;
			escrow_total += escrow;
			loan_balance -= monthly_payment-tmp1-escrow;
			interest += tmp1;
			months++;
			if(months == max_months)
				break;
			tmp1 = nominal_interest_rate*loan_balance;
		}
		//do final closeout
		if(loan_balance != 0 && months < max_months){
			tmp1 = nominal_interest_rate*loan_balance;
			//if(loan_balance < monthly_payment){
			//	principal += loan_balance-tmp1;
			//}else{
			//	principal += loan_balance-tmp1-escrow;
			//	escrow_total += escrow;
			//}

			principal += loan_balance;
			payment += loan_balance+tmp1;

			//escrow_total += escrow;
			loan_balance = 0;
			interest += tmp1;
			months++;
		}
	}else
		printf("error\n");

/*
	if(loan_balance > 0){
	//calculate final payment
		tmp1 = nominal_interest_rate*loan_balance;
		tmp2 = loan_balance_orig - principal;
		principal += tmp2;
		//payment += (monthly_payment - tmp1) + tmp1;
		escrow_total += escrow;
		payment += (tmp1+tmp2);
		interest += tmp1;
		loan_balance -= tmp2;
		months++;
	}else
		printf("error\n");

	printf("months: %u\n", months);
	printf("loan_balance: %.03lf\n", loan_balance);
	printf("payment: %.03lf\n", payment);
	printf("interest: %.03lf\n", interest);
	printf("principal: %.03lf\n", principal);
	printf("escrow: %.03lf\n", escrow);
	printf("escrow_total: %.03lf\n", escrow_total);
*/
        printf("months: %u\n", months);
        printf("loan_balance: %.03lf\n", loan_balance);
        printf("payment: %.03lf\n", payment);
        printf("interest: %.03lf\n", interest);
        printf("principal: %.03lf\n", principal);
	printf("escrow_total: %.03lf\n", escrow_total);
//        printf("fee_total: %.03lf\n", fee_t);
//        printf("tax_total: %.03lf\n", tax_t);

        tmp1 = interest+principal+escrow_total;
        printf("start balance: %.03lf\n", loan_balance_orig);
        printf("total cost: %.03lf\n", tmp1);
        printf("difference: %0.3lf\n", tmp1-loan_balance_orig);



	return 0;
}
