package me.teridax.jcash.banking.accounts;

import me.teridax.jcash.Logging;

/**
 * Immutable currency account storing only overdraft.
 * English equivalent to "Girokonto"
 */
public final class CurrentAccount extends Account {

    /**
     * Overdraft amount in currency.
     */
    private final double overdraft;

    public CurrentAccount(int iban, int pin, double balance, double overdraft) {
        super(iban, pin, balance);
        this.overdraft = overdraft;
    }

    public double getOverdraft() {
        return overdraft;
    }

    /**
     * Takeoff a certain amount of money from this accounts balance.
     * Saturates the result if the value to subtract is greater than the balance present.
     *
     * @param amount the amount of money to subtract from the accounts balance
     * @throws IllegalArgumentException if amount is smaller than 0 or the overflow is greater than the overdraft.
     */
    @Override
    public void takeoff(double amount) throws IllegalArgumentException {
        Logging.LOGGER.fine("taking off money: " + amount + " from account: " + iban);

        var overflow = amount - getBalance();

        if (overflow > 0) {
            Logging.LOGGER.fine("taking off money with overflow: " + overflow);

            if (overflow > overdraft) {
                Logging.LOGGER.warning("amount to takeoff greater than overdraft");
                throw new IllegalArgumentException("amount to takeoff greater than overdraft");
            }
        }

        this.balance -= amount;
    }
}
