package vismon.gui;

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

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class IndicatorsPanel extends JPanel{

    private JPanel mainPanel;

    private JCheckBox allCBox;
    private List<JCheckBox> indicatorCheckBoxes;

    IndicatorsPanel() {
        super();

        indicatorCheckBoxes = new ArrayList<JCheckBox>();

        init();
    }

    private void init() {
        mainPanel = new JPanel();
        JScrollPane mainScrollPane = new JScrollPane(mainPanel);

        this.setLayout(new BorderLayout());
        this.add(mainScrollPane);
    }

    public void addIndicatorCheckBoxes() {
        int rowHeight = 30;
        double mainLayout[][] = new double[2][];
        mainLayout[0] = new double[]{TableLayoutConstants.FILL};           // COLUMNS
        mainLayout[1] = new double[Session.getIndicatorNum()+1];
        for (int i = 0; i < mainLayout[1].length; ++i)
            mainLayout[1][i] = rowHeight;
        mainPanel.setLayout(new TableLayout(mainLayout));

        addAllCheckBox();

        for (int i = 0; i < Session.getIndicatorNum(); ++i)
            addIndicatorCheckBox(Session.getIndicator(i).getName() +
                    " (" + Session.getIndicator(i).getShortName() + ")", (i+1));
    }

    private void addAllCheckBox() {
        JPanel panel = new JPanel(new BorderLayout());

        allCBox = new JCheckBox("All");
        allCBox.setSelected(true);
        allCBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                for (int i = 0; i < Session.getIndicatorNum(); ++i)
                    Session.getIndicator(i).setVisible(allCBox.isSelected());
                Vismon.getVismonInstance().updateIndicatorVisibility();
            }
        });
        panel.add(allCBox);

        mainPanel.add(panel, "0,0");
    }

    private void addIndicatorCheckBox(String name, final int rowNumber) {
        JPanel panel = new JPanel();
        double panelLayout[][] = {{20, TableLayoutConstants.FILL},                                 // COLUMNS
                                {TableLayoutConstants.FILL}};                   // ROWS
        panel.setLayout(new TableLayout(panelLayout));

        JLabel blankLabel = new JLabel("  ");
        panel.add(blankLabel, "0,0");

        final JCheckBox indicatorCBox = new JCheckBox(name);
        indicatorCBox.setSelected(true);
        indicatorCBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionEvent) {
                Session.getIndicator(rowNumber-1).setVisible(indicatorCBox.isSelected());
                Vismon.getVismonInstance().updateIndicatorVisibility();
            }
        });
        indicatorCheckBoxes.add(indicatorCBox);
        panel.add(indicatorCBox, "1,0");

        mainPanel.add(panel, "0," + rowNumber);
    }

    public void allCBoxSelectedChanged() {
        boolean selected = allCBox.isSelected();
        for (JCheckBox indicatorCBox : indicatorCheckBoxes)
            indicatorCBox.setSelected(selected);
    }

    public void indicatorCBoxSelectedChanged() {
        boolean allSeleted = true;
        for (JCheckBox indicatorCBox : indicatorCheckBoxes)
            if (!indicatorCBox.isSelected()) {
                allSeleted = false;
                break;
            }
        allCBox.setSelected(allSeleted);
    }

    public void setIndicatorCBoxSelected(int i, boolean selected) {
        indicatorCheckBoxes.get(i).setSelected(selected);
        indicatorCBoxSelectedChanged();
    }

    public void setAllCBoxSelected(boolean selected) {
        allCBox.setSelected(selected);
        allCBoxSelectedChanged();
    }

}
