package cpsc2150.banking.controllers;

import cpsc2150.banking.models.*;
import cpsc2150.banking.views.*;

public class MortgageGUIController implements IMortgageController {

    // Private variables, view
    private IMortgageView view;

    public MortgageGUIController(IMortgageView v) {

        // Set the controller's reference to the view to the
        // parameterized view
        view = v;

    }

    @Override
    public void submitApplication() {

        // Get the user's name
        String name = view.getName();
        // Input validation
        if (name.isEmpty()) {

            // Print error message
            view.printToUser("A name must be provided.");

            // Return early (since a loop for input validation
            // doesn't make sense here)
            return;

        }

        // Get the user's yearly income
        double yearlyIncome = view.getYearlyIncome();
        // Input validation
        if (yearlyIncome <= 0) {

            // Print error message
            view.printToUser("Income must be greater than 0.");

            // Return early (since a loop for input validation
            // doesn't make sense here)
            return;

        }

        // Get the user's monthly debt payments
        double debtPayments = view.getMonthlyDebt();
        // Input validation
        if (debtPayments < 0) {

            // Print error message
            view.printToUser("Debt must be greater than or equal to 0.");

            // Return early (since a loop for input validation
            // doesn't make sense here)
            return;

        }

        // Get the user's credit score
        int creditScore = view.getCreditScore();
        // Input validation
        if (creditScore <= 0 || creditScore >= (IMortgage.GREATCREDIT - IMortgage.BADCREDIT + IMortgage.FAIRCREDIT)) {

            // Print error message
            view.printToUser("Credit Score must be greater than 0 and less than 850");

            // Return early (since a loop for input validation
            // doesn't make sense here)
            return;

        }

        // Get the user's house cost
        double houseCost = view.getHouseCost();
        // Input validation
        if (houseCost <= 0) {

            // Print error message
            view.printToUser("Cost must be greater than 0.");

            // Return early (since a loop for input validation
            // doesn't make sense here)
            return;

        }

        // Get the user's down payment
        double downPayment = view.getDownPayment();
        // Input validation
        if (downPayment <= 0 || downPayment >= houseCost) {

            // Print error message
            view.printToUser("Down Payment must be greater than 0 and less than the cost of the house.");

            // Return early (since a loop for input validation
            // doesn't make sense here)
            return;

        }

        // Get the user's mortgage length in years
        int years = view.getYears();

        // Create a new mortgage object by passing the house cost, down payment, number of years, and
        // a constructor call to Customer (passing debt, income, credit score, and name)
        IMortgage mortgage = new Mortgage(houseCost, downPayment, years,
                                          new Customer(debtPayments, yearlyIncome, creditScore, name));

        // If the mortgage is approved, display approval status along with
        // the mortgage rate and payment
        if (mortgage.loanApproved()) {

            view.displayApproved(true);
            view.displayRate(mortgage.getRate());
            view.displayPayment(mortgage.getPayment());

            // Otherwise, display approval status along with 0 for
            // the mortgage rate and payment
        } else {

            view.displayApproved(false);
            view.displayRate(0);
            view.displayPayment(0);

        }

    }

}