package vismon.gui;

import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstants;
import vismon.component.VerticalLabelUI;
import vismon.data.DataItem;
import vismon.data.Indicator;
import vismon.data.Session;
import vismon.plot.ScatterPlot;
import vismon.util.Constants;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ScatterPlotFrame {

    private JFrame frame;
    private JPanel mainPanel;

    private ScatterPlot scatterPlot;
    private Indicator xInd;
    private Indicator yInd;

    public ScatterPlotFrame(Indicator xInd, Indicator yInd) {
        scatterPlot = new ScatterPlot(xInd, yInd);
        this.xInd = xInd;
        this.yInd = yInd;

        Vismon.getVismonInstance().addScatterPlot(scatterPlot);
    }

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

    protected void setUp() {
        initComponents();
    }

    protected void start() {
        addPanel();

        frame.setVisible(true);
    }

    protected void initComponents() {
        int width = 500;
        int height = 500;

        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");
        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().removeScatterPlot(scatterPlot);
            }
        });
        this.frame.getContentPane().setLayout(new TableLayout(new double[][]{{0, -1, 0}, {0, -1, 0}}));
    }

    protected void addPanel() {
        mainPanel = new JPanel();
        mainPanel.setBackground(Color.WHITE);

        int borderWidth = 30;
        double[][] mainPanelLayout = new double[][]{{borderWidth, TableLayoutConstants.FILL, borderWidth},
                {borderWidth, TableLayoutConstants.FILL, borderWidth}};
        mainPanel.setLayout(new TableLayout(mainPanelLayout));
        addLabels();
        addPlot();

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

    protected void addLabels() {
        JLabel xIndicatorLabel = new JLabel(scatterPlot.getXIndicator().getName(), SwingConstants.CENTER);
        xIndicatorLabel.setFont(Constants.OVERVIEW_TITLE_FONT);
        xIndicatorLabel.setForeground(Constants.MAIN_COLOR);

        mainPanel.add(xIndicatorLabel, "1,2");

        JLabel yIndicatorLabel = new JLabel(scatterPlot.getYIndicator().getName(), SwingConstants.CENTER);
        yIndicatorLabel.setUI(new VerticalLabelUI(false));
        yIndicatorLabel.setFont(Constants.OVERVIEW_TITLE_FONT);
        yIndicatorLabel.setForeground(Constants.MAIN_COLOR);

        mainPanel.add(yIndicatorLabel, "0,1");

    }

    protected void addPlot() {
        scatterPlot.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent mouseEvent) {
                scatterPlot.mouseClicked(mouseEvent.getX(), mouseEvent.getY());
            }

            public void mousePressed(MouseEvent mouseEvent) {
            }

            public void mouseReleased(MouseEvent mouseEvent) {
            }

            public void mouseEntered(MouseEvent mouseEvent) {
            }

            public void mouseExited(MouseEvent mouseEvent) {
            }
        });
        scatterPlot.addMouseMotionListener(new MouseMotionListener() {
            public void mouseDragged(MouseEvent mouseEvent) {
            }

            public void mouseMoved(MouseEvent mouseEvent) {
                scatterPlot.setMouseOnDataItem(mouseEvent.getX(), mouseEvent.getY());
                scatterPlot.repaint();
            }
        });
        mainPanel.add(scatterPlot, "1,1");
    }

}
