package me.teridax.jcash.gui.deposit;

import me.teridax.jcash.Logging;
import me.teridax.jcash.banking.accounts.Account;
import me.teridax.jcash.gui.InvalidInputException;
import me.teridax.jcash.gui.Utils;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.text.ParseException;

public class DepositController {

    private final DepositView view;
    private final Account account;
    private final DepositData data;

    public DepositController(Account account) {
        this.account = account;

        this.data = new DepositData(account.getBalance());
        this.view = new DepositView(this.data.getMaxValue());

        this.view.getDeposit().addActionListener(e -> depositMoney());
        this.view.getCancel().addActionListener(e -> view.dispose());
        this.view.getValue().getDocument().addDocumentListener(new DocumentListener() {
            private void validateInputState() {
                var balance = account.getBalance();
                try {
                    view.getValue().commitEdit();
                    var amount = view.getAmount();
                    view.setCommittedValue(amount, balance + amount);
                    view.getDeposit().setEnabled(true);
                } catch (InvalidInputException | ParseException ex) {
                    view.setCommittedValue(0, balance);
                    view.getDeposit().setEnabled(false);
                }
            }

            @Override
            public void insertUpdate(DocumentEvent documentEvent) {
                validateInputState();
            }

            @Override
            public void removeUpdate(DocumentEvent documentEvent) {
                validateInputState();
            }

            @Override
            public void changedUpdate(DocumentEvent documentEvent) {
                validateInputState();
            }
        });
        this.view.showDialog();
    }

    private void depositMoney() {
        try {
            var amount = view.getAmount();
            Logging.LOGGER.fine("Depositing money of account: " + account.getIban() + " amount: " + amount);
            account.deposit(amount);
        } catch (IllegalArgumentException ex) {
            Logging.LOGGER.severe("Cannot deposit money of account: " + account.getIban() + " because: " + ex.getMessage());
            Utils.error(ex.getMessage());
        } catch (InvalidInputException ex) {
            Utils.error(ex.getMessage());
        }
        view.dispose();
    }
}
