/// <summary>
    /// Now that we have the info for the most money that can be borrowed, update the loan object with that "max" info.
    /// Then compute the payments based on the rates for various items.
    /// </summary>
    private static void UpdateLoanInfo(LoanProviderInfo masterLoanSpecificInfo, LoanCalculationResults theCalculationResults, decimal downPaymentPercentage)
    {
        masterLoanSpecificInfo.LoanAmount_Base             = theCalculationResults.RecommendedLoanAmount_Base;
        masterLoanSpecificInfo.PrincipleAndInterestPayment = theCalculationResults.RecommendedPayment_PrincipleAndInterest;

        //if (masterLoanSpecificInfo.GetType.Equals("USDA 30-year Loan", StringComparison.OrdinalIgnoreCase) = true)
        masterLoanSpecificInfo.UpfrontMortgageInsurancePayment = (decimal)((masterLoanSpecificInfo.UpfrontMortgageInsuranceRate / 100) * (double)masterLoanSpecificInfo.LoanAmount_Base);

        decimal annualMortgageInsurancePayment = (decimal)((masterLoanSpecificInfo.MonthlyMortgageInsuranceRate / 100) * (double)(masterLoanSpecificInfo.LoanAmount_Base + masterLoanSpecificInfo.UpfrontMortgageInsurancePayment));

        masterLoanSpecificInfo.MonthlyMortgageInsurancePayment = annualMortgageInsurancePayment / 12;

        decimal annualPropertyTaxPayment = (decimal)((masterLoanSpecificInfo.MonthlyPropertyTaxRate / 100) * (double)(masterLoanSpecificInfo.LoanAmount_Base + masterLoanSpecificInfo.UpfrontMortgageInsurancePayment));

        masterLoanSpecificInfo.MonthlyPropertyTaxPayment = annualPropertyTaxPayment / 12;

        // Add all the new payments into the loan amount.
        masterLoanSpecificInfo.LoanAmount_Actual = masterLoanSpecificInfo.LoanAmount_Base + masterLoanSpecificInfo.UpfrontMortgageInsurancePayment;
        masterLoanSpecificInfo.TotalLoanPayment  = masterLoanSpecificInfo.PrincipleAndInterestPayment + masterLoanSpecificInfo.MonthlyMortgageInsurancePayment + masterLoanSpecificInfo.MonthlyPropertyTaxPayment;

        int    salesPriceIndex     = masterLoanSpecificInfo.getRowForTableLookups(downPaymentPercentage);
        double downPaymentModifier = SalesPriceCalculationData.SalesPriceCalculation[salesPriceIndex];

        masterLoanSpecificInfo.HouseAmount_Actual = (decimal)((double)masterLoanSpecificInfo.LoanAmount_Actual / downPaymentModifier);
    }
    /// <summary>
    /// Find how much the user can borrow for this loan type.
    /// </summary>
    public static void FindBestLoanAmount(LoanProviderInfo loanProviderInfo, int maxPayment, decimal downPaymentPercentage)
    {
        LoanCalculationResults calculationResult = new LoanCalculationResults();

        calculationResult = FindBestLoanBasedOnMonthlyPayment(maxPayment, loanProviderInfo.LoanTermMonths, loanProviderInfo.InterestRate_Annual_Total, loanProviderInfo.UpfrontMortgageInsuranceRate, loanProviderInfo.MonthlyMortgageInsuranceRate, loanProviderInfo.MonthlyPropertyTaxRate);
        UpdateLoanInfo(loanProviderInfo, calculationResult, downPaymentPercentage);
    }
    /// <summary>
    /// Based on the maximum-allowable-montly-payment, find the most that can be borrowed for this loan type.
    ///   Iterate through scenarios based on increasing loan amount.
    ///   If the scenario hasn't exceeded the max payment, remember the info from the iteration and keep going.
    ///   Once the max payment is exceeded, we know to stop and use the last iteration's data.
    ///   In other words, go until you fail, then back up to the last success.
    /// </summary>
    private static LoanCalculationResults FindBestLoanBasedOnMonthlyPayment(int maxPayment, double loanTerm_Months, double interestRate_Annual, double upfrontRate_MI, double monthlyRate_MI, double monthlyRate_PropTax)
    {
        LoanCalculationResults calculationResults = new LoanCalculationResults();

        bool    keepLooking            = true;
        int     index                  = 0;
        int     loanInitialAmount      = LoanStartingAmount;
        int     loanIncrementAmount    = LoanIncrementalAmount;
        int     currentLoanAmount_Base = 0;
        int     currentLoanAmount_BasePlusMortgageInsurance = 0;
        decimal pAndIPayment      = 0;
        decimal upfrontPayment_MI = 0;
        decimal monthlyPayment_MI = 0;
        decimal annualMortgageInsurancePayment = 0;
        decimal monthlyPayment_PropTax         = 0;
        decimal annualPayment_PropTax          = 0;

        while (keepLooking == true)
        {
            currentLoanAmount_Base = (loanInitialAmount + (index * loanIncrementAmount));

            upfrontPayment_MI = ((decimal)((upfrontRate_MI / 100) * (double)currentLoanAmount_Base));

            annualMortgageInsurancePayment = (decimal)((monthlyRate_MI / 100) * (double)(currentLoanAmount_Base + upfrontPayment_MI));
            monthlyPayment_MI = annualMortgageInsurancePayment / 12;

            annualPayment_PropTax  = (decimal)((monthlyRate_PropTax / 100) * (double)(currentLoanAmount_Base + upfrontPayment_MI));
            monthlyPayment_PropTax = annualPayment_PropTax / 12;

            currentLoanAmount_BasePlusMortgageInsurance = currentLoanAmount_Base + (int)upfrontPayment_MI;
            pAndIPayment = CalculatePrincipleAndInterestPayment(currentLoanAmount_BasePlusMortgageInsurance, loanTerm_Months, interestRate_Annual);



            if ((pAndIPayment + monthlyPayment_MI + monthlyPayment_PropTax) > maxPayment)
            {
                // We have exceeded the payment maximum, so this loan info isn't valid or desired.
                // We want the info that was saved off from the previous loan calculation iteration.
                keepLooking = false;  // max payment exceeded - Stop iterating.
            }
            else
            {
                // We haven't exceeded the payment maximum, so remember these values. (Keep going!)
                // The following lines "save the data" from this iteration in case this data is the last valid data iteration.
                calculationResults.RecommendedLoanAmount_Base = currentLoanAmount_Base;

                calculationResults.RecommendedPayment_PrincipleAndInterest = pAndIPayment;

                index++;  // Keep going.
            }
        }
        return(calculationResults);
    }