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


void Usage(void){
	printf("./loan [loan_balance] [interest_rate] [monthly_payment] [yearly fee] [yearly tax] [cur month] [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;
*/

void Inc_Month(uint32_t *month){
	uint32_t tmp1 = *month;
	if(tmp1 == 12)
		tmp1 = 1;
	else
		tmp1++;
	*month = tmp1;
}
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 fee = 0;
	double tax = 0;
	double fee_t = 0;
	double tax_t = 0;
	uint32_t month = 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 years = 0;
	//uint32_t num1 = 0;

	if(argc != 8){
		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]);

	fee = atof(argv[4]);
	tax = atof(argv[5]);
	month = atoi(argv[6]);
	lump = atof(argv[7]);

	//monthly_payment -= escrow;
	printf("loan_balance: %.03lf\n", loan_balance);

	if(lump != 0){
		if(month == 10){
			loan_balance += fee;
			fee_t += fee;
			tax_t += tax;
		}
		tmp1 = nominal_interest_rate*loan_balance;
		//tmp2 = lump - tmp1;
		//escrow_total += escrow;
		principal += lump-tmp1;
		//principal += tmp2;
		interest += tmp1;
		payment += lump;
		//printf("loan_balance: %.03lf\n", loan_balance);
		if(month == 10)
			loan_balance -= tax;
		loan_balance -= lump-tmp1;
		months++;
		//month++;
		printf("loan_balance: %.03lf\n", loan_balance);
		Inc_Month(&month);
	}
/*
	while((loan_balance-monthly_payment) > 0){
		if(month == 10){
			//fee debit, interest, tax credit 
			loan_balance += fee;
			fee_t += fee;
			tax_t += tax;
		}
		tmp1 = nominal_interest_rate*loan_balance;
		//tmp2 = (monthly_payment - tmp1);
		//escrow_total += escrow;
		//principal += tmp2;
		principal += monthly_payment-tmp1;
		interest += tmp1;
		payment += monthly_payment;

		if(month == 10)
			loan_balance -= tax;
		loan_balance -= monthly_payment-tmp1;

		months++;
		month++;
	}
*/
	if(loan_balance > 0){
	//calculate final payment
		//printf("loan_balance: %.03lf\n", loan_balance);
		if(month == 10){
			loan_balance += fee;
			fee_t += fee;
			//tax_t += tax;
		}
		tmp1 = nominal_interest_rate*loan_balance;

		while((monthly_payment-tmp1) < loan_balance){
			//principal += loan_balance;
			//payment += loan_balance;
			//if(month == 10){
			//	loan_balance += fee;
			//	fee_t += fee;
			//}
			principal += monthly_payment-tmp1;
			payment += monthly_payment;
			loan_balance -= monthly_payment-tmp1;
			if(month == 10){
				if(tax < loan_balance){
					loan_balance -= tax;
					tax_t += tax;
				}else{
					//tax credit closes out contract
					printf("warning: final tax section!\n");
					tax_t += loan_balance;
					loan_balance = 0;
				}
			}
			interest += tmp1;
			months++;
			//month++;
			Inc_Month(&month);
			if(month == 10 && loan_balance != 0){
				loan_balance += fee;
				fee_t += fee;
			}
			printf("loan_balance: %.03lf\n", loan_balance);
			tmp1 = nominal_interest_rate*loan_balance;
		}
		//do final closeout
		if(loan_balance != 0){
			if(month == 10){
				loan_balance += fee;
				fee_t += fee;
			}
			tmp1 = nominal_interest_rate*loan_balance;
			//loan_balance -= monthly_payment-tmp1;
			principal += loan_balance;
			payment += loan_balance+tmp1;
			loan_balance = 0;
			interest += tmp1;
			months++;
			//month++;
			Inc_Month(&month);
			printf("loan_balance: %.03lf\n", loan_balance);
		}
	}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("fee_total: %.03lf\n", fee_t);
	printf("tax_total: %.03lf\n", tax_t);

	tmp1 = interest+principal+tax_t;
	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;
}
