package me.teridax.jcash.gui.transfer;

import me.teridax.jcash.Logging;
import me.teridax.jcash.banking.management.BankingManagementSystem;
import me.teridax.jcash.decode.StringDecoder;

public class TransferData {

    private final BankingManagementSystem bms;

    public TransferData(BankingManagementSystem bms) {
        this.bms = bms;
    }

    /**
     * Transfers a certain amount of money to the specified account of the specified bank
     *
     * @param amount     the amount to transfer
     * @param blz        the bank that manages the account
     * @param ibanString the internal bank account number to transfer money to
     * @throws IllegalArgumentException if the bank or the account do not exist
     */
    public void transferValue(double amount, String blz, String ibanString) throws IllegalArgumentException {
        // get bank to transfer to
        var bank = bms.getBank(blz);
        if (bank.isEmpty()) {
            Logging.LOGGER.warning("Bank not found: " + blz);
            throw new IllegalArgumentException("Bank not found: " + blz);
        }

        // validate iban of target account
        var iban = 0;
        try {
            iban = StringDecoder.decodeUniqueIdentificationNumber(ibanString);
        } catch (Exception ex) {
            Logging.LOGGER.warning("IBAN has invalid format: " + ibanString + " because: " + ex.getMessage());
            throw new IllegalArgumentException("IBAN has invalid format: " + ibanString);
        }

        // get account to transfer value to
        var account = bank.get().getAccount(iban);
        if (account.isEmpty()) {
            Logging.LOGGER.warning("Account not found: " + iban);
            throw new IllegalArgumentException("Account not found: " + iban);
        }

        account.get().getPrimaryAccount().deposit(amount);
    }
}
