package vismon.gui;


import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstants;
import vismon.data.Indicator;
import vismon.data.Session;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class IndicatorSelector {

    private JFrame frame;

    private JPanel mainPanel;
    private JPanel northPanel;
    private JPanel centrePanel;
    private JPanel southPanel;

    private JComboBox indicatorComboBox;

    private Indicator firstIndicator;

    public IndicatorSelector(Indicator indicator) {
        firstIndicator = indicator;
    }

    public void run() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                setUp();
                start();
            }
        });
    }

    protected void setUp() {
        initComponents();
    }

    protected void start() {
        addPanel();

        frame.setVisible(true);
    }

    private void initComponents() {
        int width = 420;
        int height = 120;

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int xLocation = (int)((screenSize.getWidth() - width) * 0.5);
        int yLocation = (int)((screenSize.getHeight() - height) * 0.5);

        this.frame = new JFrame("Scatter Plot Creator");
        this.frame.setSize(new Dimension(width, height));
        this.frame.setLocation(xLocation, yLocation);
        this.frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                Vismon.getVismonInstance().setPreferences();
            }
        });

        this.frame.getContentPane().setLayout(new TableLayout(new double[][]{{0, -1, 0}, {0, -1, 0}}));
        this.frame.setResizable(false);
    }

    protected void addPanel() {
        mainPanel = new JPanel();

        double mainLayout[][] = new double[2][];
        mainLayout[0] = new double[]{TableLayoutConstants.FILL};           // COLUMNS
        mainLayout[1] = new double[]{0.37, 0.37, 0.26};
        mainPanel.setLayout(new TableLayout(mainLayout));

        addGUI();

        Container frameContainer = this.frame.getContentPane();
        frameContainer.setLayout(new BorderLayout());
        frameContainer.add(mainPanel, BorderLayout.CENTER);
    }

    private void addGUI() {
        setUpPanels();
        addFirstIndLabel();
        addIndicatorElements();
        addButtons();
    }

    private void setUpPanels() {
        northPanel = new JPanel();
        mainPanel.add(northPanel, "0,0");

        centrePanel = new JPanel();
        mainPanel.add(centrePanel, "0,1");

        southPanel = new JPanel();
        mainPanel.add(southPanel, "0,2");
    }

    private void addFirstIndLabel() {
        double northLayout[][] = new double[2][];
        northLayout[0] = new double[]{TableLayoutConstants.FILL};           // COLUMNS
        northLayout[1] = new double[]{TableLayoutConstants.FILL};
        northPanel.setLayout(new TableLayout(northLayout));

        JLabel label = new JLabel(" First indicator: \"" + firstIndicator.getName() + "\"");
        northPanel.add(label, "0,0");
    }

    private void addIndicatorElements() {
        double centreLayout[][] = new double[2][];
        centreLayout[0] = new double[]{125, TableLayoutConstants.FILL};           // COLUMNS
        centreLayout[1] = new double[]{TableLayoutConstants.FILL};
        centrePanel.setLayout(new TableLayout(centreLayout));

        JLabel label = new JLabel(" Second indicator: ");
        centrePanel.add(label, "0,0");

        String[] options = new String[Session.getIndicatorNum() + 1];
        options[0] = "";
        for (int i = 0; i < Session.getIndicatorNum(); ++i)
            options[i+1] = Session.getIndicator(i).getName();

        indicatorComboBox = new JComboBox(options);
        centrePanel.add(indicatorComboBox, "1,0");
    }

    private void addButtons() {
        double southLayout[][] = new double[2][];
        southLayout[0] = new double[]{TableLayoutConstants.FILL, 150, 80};
        southLayout[1] = new double[]{TableLayoutConstants.FILL};
        southPanel.setLayout(new TableLayout(southLayout));

        JButton okButton = new JButton("Create Scatter Plot");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                int index = indicatorComboBox.getSelectedIndex();
                if (index == 0) {
                    JOptionPane.showMessageDialog(frame, "Second indicator is not selected.");
                    return;
                }
                Indicator secondIndicator = Session.getIndicator(indicatorComboBox.getSelectedIndex() - 1);
                ScatterPlotFrame scatterPlotFrame = new ScatterPlotFrame(firstIndicator, secondIndicator);
                scatterPlotFrame.run();

                frame.setVisible(false);
                frame.dispose();
            }
        });
        southPanel.add(okButton, "1,0");

        JButton cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                frame.setVisible(false);
                frame.dispose();
            }
        });
        southPanel.add(cancelButton, "2,0");
    }

}
