package vismon.gui;

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

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ScenarioDetailsDialog {

    private JFrame frame;
    private JPanel mainPanel;

    private JTable table;
    private DefaultTableModel tableModel;
    private final int columNum = Session.getIndicatorNum()+1;

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

    protected void setUp() {
        initComponents();
    }

    protected void start() {
        addPanel();
        addTable();

        frame.setVisible(true);
    }

    protected void initComponents() {
        int width = 800;
        int height = 600;

        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("Scenario Details");
        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);

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

    protected void addTable() {
        Object[] headers = new Object[columNum];
        headers[0] = "";
        for (int i = 1; i < columNum; ++i)
            headers[i] = Session.getIndicator(i-1).getShortName();

        tableModel = new DefaultTableModel();
        tableModel.setDataVector(null, headers);

        table = new JTable(tableModel);
        table.setShowGrid(true);
        table.setGridColor(Color.LIGHT_GRAY);

        for (int i = 1; i < columNum; ++i) {
            TableColumn ithColumn = table.getColumnModel().getColumn(i);
            ithColumn.setCellRenderer(new MyBoxPlotRenderer(Session.getIndicator(i-1)));
        }

        addRows();

        table.setRowHeight(50);

        JScrollPane tableScrollPane = new JScrollPane(table);

        double[][] tableLayout = new double[][]{{TableLayoutConstants.FILL},
                {TableLayoutConstants.FILL}};
        mainPanel.setLayout(new TableLayout(tableLayout));
        mainPanel.add(tableScrollPane, "0,0");
    }

    private void addRows() {
        for (Scenario scenario : Session.getScenarios()) {
            Object[] rowVector = new Object[columNum];
            rowVector[0] = scenario.getName();
            for (int i = 1; i < columNum; ++i)
                rowVector[i] = scenario;

            tableModel.addRow(rowVector);
        }
    }

    public void close() {
        frame.setVisible(false);
        frame.dispose();
    }

    private class MyBoxPlotRenderer extends BoxPlot implements TableCellRenderer {

        public MyBoxPlotRenderer(Indicator indicator) {
            super(indicator);
            setOpaque(true);
        }

        public Component getTableCellRendererComponent(JTable table, Object scenario, boolean isSelected, boolean hasFocus, int row, int column) {
            this.setScenario((Scenario) scenario);
            setToolTipText(getToolTipText());
            return this;
        }
    }

}
