package cpsc2150.banking.controllers;

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

public class MortgageController implements IMortgageController {

    // Private variables, screen
    IMortgageView screen;

    public MortgageController(IMortgageView view) {

        // Set the private view to the parameterized instance
        screen = view;

    }

    @Override
    public void submitApplication() {

        // Create instances of customer and mortgage
        ICustomer customer;
        IMortgage mortgage;

        // Status variables to determine whether to keep looping
        boolean newCustomer = true;
        boolean newHouse = true;

        // Loop while a new customer should be entered
        while (newCustomer) {

            // Double-check that newHouse is true so that a new house will be required for a new customer
            newHouse = true;

            // Get the user's name
            String name = screen.getName();

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

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

                // Get the user's yearly income
                yearlyIncome = screen.getYearlyIncome();

            }

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

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

                // Get the user's monthly debt payments
                debtPayments = screen.getMonthlyDebt();

            }

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

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

                // Get the user's credit score
                creditScore = screen.getCreditScore();

            }

            // Initialize the customer object with the data entered by the user
            customer = new Customer(debtPayments, yearlyIncome, creditScore, name);

            // Loop while a new mortgage should be entered
            while (newHouse) {

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

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

                    // Get the user's house cost
                    houseCost = screen.getHouseCost();

                }

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

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

                    // Get the user's down payment
                    downPayment = screen.getDownPayment();

                }

                // Get the user's mortgage length in years
                int years = screen.getYears();
                // Input validation
                while (years <= 0) {

                    // Print error message
                    screen.printToUser("Years must be greater than 0.");

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

                }

                // Initialize the mortgage object with the data entered by the user
                mortgage = new Mortgage(houseCost, downPayment, years, customer);

                // Print the details about the customer and mortgage
                screen.printToUser(customer.toString()+ mortgage.toString());

                // Prompt to enter a new mortgage
                newHouse = screen.getAnotherMortgage();

            }

            // Prompt to enter a new customer
            newCustomer = screen.getAnotherCustomer();

        }

    }

}
