package view.panels.tabs;

import com.sun.istack.internal.Nullable;
import view.panels.PlotPanel;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;

/**
 * Implementierung verschiedener Algorithmen zur Berechnung von Ausgleichsgeraden.
 *
 * @Author: Armin Wolf
 * @Email: a_wolf28@uni-muenster.de
 * @Date: 21.06.2017.
 */
public class RMPanel extends TabPanel {

    private JLabel labels;
    private JTextField input;
    private JPanel continer;
    private GridBagConstraints gbc;

    public RMPanel() {
        super();
        this.labels = new JLabel();

        this.input = new JTextField();
        this.setLayout(new BorderLayout());


        this.continer = new JPanel();
        this.continer.setLayout(new GridBagLayout());

        this.gbc = new GridBagConstraints();
        this.gbc.anchor = GridBagConstraints.NORTH;
        this.gbc.fill = GridBagConstraints.HORIZONTAL;

        addTextfieldAndInput(0, "\u00df (0 < \u00df < 1)", 0.5);


        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        gbc.insets = new Insets(30, 0, 10, 0);
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.weightx = 0.05;
        gbc.weighty = 0.05;
        buttonPanel.add(getStartButton());
        continer.add(buttonPanel, gbc);

        getNorthPanel().add(continer, BorderLayout.CENTER);
        this.add(getNorthPanel(), BorderLayout.NORTH);
        this.add(getCenterPanel(), BorderLayout.CENTER);
    }

    @Override
    protected void addTextfieldAndInput(int row, String name, Double value) {
        this.labels = new JLabel(name);
        this.labels.setFont(new Font("SansSerif", Font.PLAIN, 13));
        this.input = new JTextField();
        this.input.setText("" + value);

        gbc.insets = new Insets(0, 5, 0, 0);
        gbc.gridx = 0;
        gbc.gridy = row;
        gbc.weightx = 0.05;
        gbc.weighty = 0.05;
        continer.add(this.labels, gbc);

        gbc.gridx = 1;
        gbc.gridy = row;
        gbc.weightx = 0.9;
        gbc.weighty = 0.05;
        gbc.insets = new Insets(0, 0, 0, 5);
        continer.add(this.input, gbc);
    }


    @Nullable
    public String getInput() {
        String input = "";
        input = this.input.getText();
        if (isNumeric(input))
            return input;
        else
            JOptionPane.showMessageDialog(this, "Bitte geben Sie numerische Werte als Parameter an.", "Fehler bei der Eingabe", JOptionPane.ERROR_MESSAGE);
        return null;
    }

    public void setInput(JTextField input) {
        this.input = input;
    }


    public boolean isNumeric(String str) {
        try {
            double d = Double.parseDouble(str);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }
}
