package vismon.gui;

import info.clearthought.layout.TableLayout;
import info.clearthought.layout.TableLayoutConstants;
import vismon.component.VerticalLabelUI;
import vismon.data.Indicator;
import vismon.data.MngOption;
import vismon.data.Session;
import vismon.plot.ContourPlot;
import vismon.util.Constants;
import vismon.util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class ContourPlotFrame {

    private JFrame frame;
    private JPanel mainPanel;

    private JLabel mngOption0Label;
    private JLabel mngOption1Label;
    private JLabel indicatorLabel;
    private ContourPlot contourPlot;

    public ContourPlotFrame(Indicator indicator) {
        contourPlot = new ContourPlot(Session.getMngOption(0), Session.getMngOption(1), indicator);
        contourPlot.computeInterpolatedData();
        contourPlot.setBackground(Color.WHITE);

        contourPlot.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent mouseEvent) {
                if (mouseEvent.getButton() == MouseEvent.BUTTON1)
                    contourPlot.createScenario();
            }

            @Override
            public void mousePressed(MouseEvent mouseEvent) {
            }

            @Override
            public void mouseReleased(MouseEvent mouseEvent) {
            }

        });

        contourPlot.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent mouseEvent) {
                contourPlot.setCursorLocalX(mouseEvent.getX());
                contourPlot.setCursorLocalY(mouseEvent.getY());
                contourPlot.calcCursorXY(mouseEvent);
                contourPlot.setCrossHairScenarion();
                contourPlot.showScenarioName();

                repaintAllGUIs();
                Vismon.getVismonInstance().getCplomPanel().repaintAllGUIs();
            }
        });
    }

    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("Contour plot: " + contourPlot.getIndicator().getName());
        this.frame.setSize(new Dimension(width, height));
        this.frame.setLocation(xLocation, yLocation);
        this.frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        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() {
        indicatorLabel = new JLabel(contourPlot.getIndicator().getName(), SwingConstants.CENTER);
        indicatorLabel.setFont(Constants.OVERVIEW_TITLE_FONT);
        indicatorLabel.setForeground(Constants.MAIN_COLOR);

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

        mngOption0Label = new JLabel(Session.getMngOption(0).getName(), SwingConstants.CENTER);
        mngOption0Label.setFont(Constants.OVERVIEW_TITLE_FONT);
        mngOption0Label.setForeground(Constants.MAIN_COLOR);

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

        mngOption1Label = new JLabel(Session.getMngOption(1).getName(), SwingConstants.CENTER);
        mngOption1Label.setUI(new VerticalLabelUI(false));
        mngOption1Label.setFont(Constants.OVERVIEW_TITLE_FONT);
        mngOption1Label.setForeground(Constants.MAIN_COLOR);

        mainPanel.add(mngOption1Label, "0,1");
    }

    protected void addPlot() {
        mainPanel.add(contourPlot, "1,1");
    }

    protected void repaintAllGUIs() {
        MngOption mngOption0 = Session.getMngOption(0);
        MngOption mngOption1 = Session.getMngOption(1);

        if (Session.getCrosshairScenario() != null) {
            String mng0Text = mngOption0.getName() + ": " +
                            Util.roundToOneHundredth(Session.getCrosshairScenario().getMngAction().getMngValue(mngOption0));
            mngOption0Label.setText(mng0Text);

            String mng1Text = mngOption1.getName() + ": " +
                            Util.roundToOneHundredth(Session.getCrosshairScenario().getMngAction().getMngValue(mngOption1));
            mngOption1Label.setText(mng1Text);

            String labelText = contourPlot.getIndicator().getName() + ": "
                            + Util.roundToOneTenth(Session.getCrosshairScenario().getAverageValue(contourPlot.getIndicator()));
            indicatorLabel.setText(labelText);
        } else {
            mngOption0Label.setText(mngOption0.getName());

            mngOption1Label.setText(mngOption1.getName());

            indicatorLabel.setText(contourPlot.getIndicator().getName());
        }

        contourPlot.repaint();
    }
}
