package me.teridax.jcash.gui.transfer;

import me.teridax.jcash.Logging;
import me.teridax.jcash.decode.StringDecoder;
import me.teridax.jcash.gui.IconProvider;
import me.teridax.jcash.gui.InvalidInputException;

import javax.swing.*;
import java.awt.*;
import java.text.ParseException;

import static me.teridax.jcash.lang.Translator.translate;

public class TransferView {

    private JDialog dialog;
    private JButton cancel;
    private JButton transfer;
    private JFormattedTextField iban;
    private JFormattedTextField blz;
    private JFormattedTextField value;

    private JLabel balanceAfterTransfer;
    private JLabel enteredValue;

    public TransferView(double maxValue) {
        createComponents(maxValue);
        layoutComponents();
    }

    public void showDialog() {
        dialog.setIconImage(IconProvider.getWindowIcon());
        dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setTitle(translate("Transfer money"));
        dialog.pack();
        dialog.setSize(dialog.getWidth() * 2, dialog.getHeight());
        dialog.setResizable(false);
        dialog.setLocationRelativeTo(null);
        dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }

    private void layoutComponents() {
        var c = new GridBagConstraints();

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.weightx = 0;
        c.insets = new Insets(6,6,6,6);
        dialog.getContentPane().add(new JLabel(translate("BLZ"), SwingConstants.RIGHT), c);

        c.gridx = 1;
        c.gridy = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.weightx = 0.5;
        dialog.getContentPane().add(blz, c);

        c.gridx = 2;
        c.gridy = 0;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.weightx = 0;
        dialog.getContentPane().add(new JLabel(translate("IBAN"), SwingConstants.RIGHT), c);

        c.gridx = 3;
        c.gridy = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.weightx = 1;
        dialog.getContentPane().add(iban, c);

        c.gridx = 0;
        c.gridy = 1;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.weightx = 0;
        dialog.getContentPane().add(new JLabel(translate("Betrag"), SwingConstants.RIGHT), c);

        c.gridx = 1;
        c.gridy = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.weightx = 0.5;
        dialog.getContentPane().add(value, c);

        c.gridx = 0;
        c.gridy = 2;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.weightx = 0.5;
        dialog.getContentPane().add(new JLabel(translate("Value to transfer:"), JLabel.RIGHT), c);

        c.gridx = 1;
        c.gridy = 2;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.weightx = 0.5;
        dialog.getContentPane().add(enteredValue, c);

        c.gridx = 0;
        c.gridy = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.weightx = 0.5;
        dialog.getContentPane().add(new JLabel(translate("Balance after transfer:"), JLabel.RIGHT), c);

        c.gridx = 1;
        c.gridy = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.weightx = 0.5;
        dialog.getContentPane().add(balanceAfterTransfer, c);

        var buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        buttonPanel.add(cancel);
        buttonPanel.add(transfer);

        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 4;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        dialog.getContentPane().add(buttonPanel, c);
    }

    private void createComponents(double maxValue) {
        this.dialog = new JDialog();

        this.cancel = new JButton(translate("Cancel"));
        this.transfer = new JButton(translate("Transfer"));
        this.transfer.setEnabled(false);
        this.value = new JFormattedTextField(StringDecoder.getNumberFormatter(maxValue));
        this.iban = new JFormattedTextField();
        this.blz = new JFormattedTextField();
        this.enteredValue = new JLabel();
        this.balanceAfterTransfer = new JLabel(StringDecoder.getNumberFormat().format(maxValue));

        this.dialog.setContentPane(new JPanel(new GridBagLayout()));
    }

    public double getAmount() throws InvalidInputException {
        if (value.getText().isBlank())
            throw new InvalidInputException("currency value is blank or has been invalid whilst entered");

        try {
            return StringDecoder.getNumberFormat().parse(value.getText()).doubleValue();
        } catch (ParseException e) {
            Logging.LOGGER.severe("Amount text field contains invalid value: " + value);
            throw new InvalidInputException(e);
        }
    }

    public void setCommittedValue(double amount, double after) {
        enteredValue.setText(StringDecoder.getNumberFormat().format(amount));
        balanceAfterTransfer.setText(StringDecoder.getNumberFormat().format(after));
    }

    public JFormattedTextField getValue() {
        return value;
    }

    public JButton getCancel() {
        return cancel;
    }

    public JButton getTransfer() {
        return transfer;
    }

    public String getIban() {
        return iban.getText();
    }

    public String getBlz() {
        return blz.getText();
    }

    public void dispose() {
        this.dialog.dispose();
    }
}
