/*
 * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
 */
package ch.psi.plot.jfree;

import ch.psi.plot.jfree.MatrixPlot;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 *
 */
public class ManualScaleDialog extends JDialog {

    // Default serial id
    private static final long serialVersionUID = 1L;

    private final JPanel contentPanel = new JPanel();
    private JTextField textFieldHighValue;
    private JTextField textFieldLowValue;

    private MatrixPlot plot;

    private int selectedOption;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            ManualScaleDialog dialog = new ManualScaleDialog();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
            System.out.println("HERE");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public ManualScaleDialog() {
        setModal(true); // Block until dialog is disposed.
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            JLabel lblLowValue = new JLabel("Low Value:");
            lblLowValue.setFont(new Font("Lucida Grande", Font.BOLD, 13));
            contentPanel.add(lblLowValue);
        }
        {
            textFieldLowValue = new JTextField();
            contentPanel.add(textFieldLowValue);
            textFieldLowValue.setColumns(10);
        }
        {
            JLabel lblHighValue = new JLabel("High Value:");
            lblHighValue.setFont(new Font("Lucida Grande", Font.BOLD, 13));
            contentPanel.add(lblHighValue);
        }
        {
            textFieldHighValue = new JTextField();
            contentPanel.add(textFieldHighValue);
            textFieldHighValue.setColumns(10);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        // set OK
                        selectedOption = JOptionPane.OK_OPTION;
                        dispose();
                    }
                });
                {
                    JButton btnApply = new JButton("Apply");
                    btnApply.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent arg0) {
                            // TODO apply values
                            if (plot != null) {
                                plot.setColorScale(getLow(), getHigh());
                            }
                        }
                    });
                    buttonPane.add(btnApply);
                }
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        // set Cancel
                        selectedOption = JOptionPane.CANCEL_OPTION;
                        dispose();
                    }
                });
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        pack();
    }

    public void showDialog() {
        setVisible(true);
    }

    public void setMatrixPlot(MatrixPlot p) {
        plot = p;
    }

    public void setLow(double low) {
        textFieldLowValue.setText(String.format("%.4f", low));
    }

    public void setHigh(double high) {
        textFieldHighValue.setText(String.format("%.4f", high));
    }

    public double getLow() {
        double low = Double.NaN;
        try {
            low = Double.parseDouble(textFieldLowValue.getText());
        } catch (NumberFormatException e) {
        }
        return low;
    }

    public double getHigh() {
        double high = Double.NaN;
        try {
            high = Double.parseDouble(textFieldHighValue.getText());
        } catch (NumberFormatException e) {
        }
        return high;
    }

    /**
     * @return the selectedOption
     */
    public int getSelectedOption() {
        return selectedOption;
    }

}
