package me.teridax.jcash.gui.login;

import me.teridax.jcash.Logging;
import me.teridax.jcash.banking.management.BankingManagementSystem;
import me.teridax.jcash.banking.management.Profile;
import me.teridax.jcash.gui.Utils;

import java.util.Optional;

/**
 * Wrapper class for a {@link BankingManagementSystem}
 */
public class LoginData {

    private BankingManagementSystem bms;

    /**
     * authenticate the specified account with the provided pin.
     *
     * @param blz  the bank identifier
     * @param iban the account identifier
     * @param pin  the pin for the account to authenticate with
     * @return an optional wrapping the specified account if authentication was successful
     */
    public Optional<Profile> authenticateAccount(String blz, int iban, int pin) {
        Logging.LOGGER.info("Authenticating account " + iban);

        var optionalBank = bms.getBank(blz);
        if (optionalBank.isEmpty()) {
            Utils.error("Unknown BLZ: " + blz);
            return Optional.empty();
        }

        var profile = optionalBank.get().getAccount(iban);
        if (profile.isEmpty()) {
            Utils.error("Unknown account: " + iban);
            return Optional.empty();
        }

        var account = profile.filter(value -> value.getPrimaryAccount().getPin() == pin);
        if (account.isEmpty()) {
            Utils.error("PIN does not match");
            return Optional.empty();
        }

        return account;
    }

    public BankingManagementSystem getBms() {
        return bms;
    }

    public void setBms(BankingManagementSystem bms) {
        this.bms = bms;
    }
}
