package me.teridax.jcash.gui.takeoff;

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 TakeoffView {

    private JDialog dialog;
    private JButton cancel;
    private JButton takeoff;
    private JLabel enteredValue;
    private JLabel balanceAfterDeposit;
    private JFormattedTextField value;

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

    public void showDialog() {
        dialog.setIconImage(IconProvider.getWindowIcon());
        dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
        dialog.setTitle(translate("Takeoff 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("Value"), 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(value, c);

        c.gridx = 2;
        c.gridy = 0;
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.LINE_START;
        c.weightx = 0;
        dialog.getContentPane().add(new JLabel("€"), c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        dialog.getContentPane().add(new JLabel(translate("Value to takeoff:"), SwingConstants.RIGHT), c);

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

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        dialog.getContentPane().add(new JLabel(translate("Balance after takeoff:"), SwingConstants.RIGHT), c);

        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 2;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        dialog.getContentPane().add(balanceAfterDeposit, c);

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

        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 3;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.anchor = GridBagConstraints.LAST_LINE_END;
        c.insets = new Insets(10, 10, 10, 10);
        dialog.getContentPane().add(buttonPanel, c);
    }

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

        this.cancel = new JButton(translate("Cancel"));
        this.takeoff = new JButton(translate("Takeoff"));
        this.value = new JFormattedTextField(StringDecoder.getNumberFormatter(maxValue));
        this.enteredValue = new JLabel();
        this.balanceAfterDeposit = 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));
        balanceAfterDeposit.setText(StringDecoder.getNumberFormat().format(after));
    }

    public JButton getCancel() {
        return cancel;
    }

    public JButton getTakeoff() {
        return takeoff;
    }

    public JFormattedTextField getValue() {
        return value;
    }

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