/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Import.java
 *
 * Created on Nov 20, 2009, 1:36:47 PM
 */

package grader.UI.file;

import java.awt.CardLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Nick
 */
public class Import extends javax.swing.JDialog {
    private int currentDisplayNum = 1;
    /** Creates new form Import */
    public Import(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        mainPanel = new JPanel();
        welcomePanel = new JPanel();
        welcomeLabel = new JLabel();
        infoLabel = new JLabel();
        continueLabel = new JLabel();
        defaultCheckbox = new JCheckBox();
        importPanel = new JPanel();
        importLabel = new JLabel();
        mergeCheckbox = new JCheckBox();
        summaryPanel = new JPanel();
        sucessLabel = new JLabel();
        unsuccessLabel = new JLabel();
        successfulScrollPane = new JScrollPane();
        unsuccessfulScrollPane = new JScrollPane();
        backButton = new JButton();
        nextButton = new JButton();
        cancelButton = new JButton();
        jSeparator1 = new JSeparator();
        jTableImport = new JTable();
        courses = new String[][] {{"CSC 308", "01"},{"CSC 308", "02"},{"CPE 101", "09"}};

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form");
        setResizable(false);

        mainPanel.setName("mainPanel");
        mainPanel.setLayout(new java.awt.CardLayout());

        welcomePanel.setName("welcomePanel");
        welcomeLabel.setText("Welcome to Grader!");
        welcomeLabel.setName("welcomeLabel");

        infoLabel.setText("This wizard will import one or more classes in SIS format.");
        infoLabel.setName("infoLabel");

        continueLabel.setText("Click Next to continue.");
        continueLabel.setName("continueLabel");

        defaultCheckbox.setText("Import classes using defaults");
        defaultCheckbox.setName("defaultsCheckbox");

        initTable();
        initSummaryTable();
        importScrollPane = new JScrollPane(jTableImport);
        successfulScrollPane = new JScrollPane(jTableSuccess);
        unsuccessfulScrollPane = new JScrollPane(jTableUnsuccess);


        org.jdesktop.layout.GroupLayout welcomePanelLayout = new org.jdesktop.layout.GroupLayout(welcomePanel);
        welcomePanel.setLayout(welcomePanelLayout);
        welcomePanelLayout.setHorizontalGroup(
            welcomePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(welcomePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(welcomePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(continueLabel)
                    .add(welcomeLabel)
                    .add(infoLabel))
                .addContainerGap(84, Short.MAX_VALUE))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, welcomePanelLayout.createSequentialGroup()
                .addContainerGap(138, Short.MAX_VALUE)
                .add(defaultCheckbox)
                .add(125, 125, 125))
        );
        welcomePanelLayout.setVerticalGroup(
            welcomePanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(welcomePanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(welcomeLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(infoLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(continueLabel)
                .add(100, 100, 100)
                .add(defaultCheckbox)
                .addContainerGap(178, Short.MAX_VALUE))
        );

        mainPanel.add(welcomePanel, "1");

        importPanel.setName("importPanel"); // NOI18N

        importLabel.setText("Check the box next to each class that you want to import from SIS."); // NOI18N
        importLabel.setName("importLabel"); // NOI18N

        importScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        importScrollPane.setName("importScrollPane");

        mergeCheckbox.setText("Merge like sections into common gradebook"); // NOI18N
        mergeCheckbox.setName("mergeCheckbox");

        org.jdesktop.layout.GroupLayout importPanelLayout = new org.jdesktop.layout.GroupLayout(importPanel);
        importPanel.setLayout(importPanelLayout);
        importPanelLayout.setHorizontalGroup(
            importPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(importPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(importPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                    .add(importLabel)
                    .add(importScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 265, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(mergeCheckbox))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        importPanelLayout.setVerticalGroup(
            importPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(importPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(importLabel)
                .add(18, 18, 18)
                .add(importScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(mergeCheckbox))
        );

        mainPanel.add(importPanel, "2");

        summaryPanel.setName("summaryPanel"); // NOI18N

        sucessLabel.setText("The following classes were successfully imported:"); // NOI18N
        sucessLabel.setName("sucessLabel"); // NOI18N

        unsuccessLabel.setText("The following classes could not be imported:"); // NOI18N
        unsuccessLabel.setName("unsuccessLabel"); // NOI18N

        successfulScrollPane.setName("successfulScrollPane"); // NOI18N

        unsuccessfulScrollPane.setName("unsuccessfulScrollPane"); // NOI18N

        org.jdesktop.layout.GroupLayout summaryPanelLayout = new org.jdesktop.layout.GroupLayout(summaryPanel);
        summaryPanel.setLayout(summaryPanelLayout);
        summaryPanelLayout.setHorizontalGroup(
            summaryPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(summaryPanelLayout.createSequentialGroup()
                .add(80, 80, 80)
                .add(summaryPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                    .add(successfulScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                    .add(sucessLabel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(unsuccessfulScrollPane, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 334, Short.MAX_VALUE)
                    .add(unsuccessLabel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 292, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(70, 70, 70))
        );
        summaryPanelLayout.setVerticalGroup(
            summaryPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, summaryPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(sucessLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(successfulScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 92, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(unsuccessLabel)
                .add(26, 26, 26)
                .add(unsuccessfulScrollPane, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 100, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(0, 21, Short.MAX_VALUE))
        );

        mainPanel.add(summaryPanel, "3");

        backButton.setText("Back");
        backButton.setEnabled(false);
        backButton.setName("backButton"); // NOI18N
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        nextButton.setText("Next");
        nextButton.setName("nextButton"); // NOI18N
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.setName("cancelButton"); // NOI18N
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jSeparator1.setName("jSeparator1"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(mainPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(208, Short.MAX_VALUE)
                .add(backButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(nextButton)
                .add(18, 18, 18)
                .add(cancelButton)
                .addContainerGap())
            .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
        );

        layout.linkSize(new java.awt.Component[] {backButton, cancelButton, nextButton}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(mainPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cancelButton)
                    .add(nextButton)
                    .add(backButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {
        int j = 0;
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        String card;
        String prevCourse = courses[0][0];
        String curCourse = courses[0][0];
        int idx = 0;
        String results[] = {"","",""};

        if (defaultCheckbox.isSelected()) {
            currentDisplayNum = 3;
            card = String.valueOf(currentDisplayNum);
            defaultCheckbox.setSelected(false);
            mergeCheckbox.setSelected(false);
            jTableImport.setValueAt(true, 0,2);
            jTableImport.setValueAt(true, 1,2);
            jTableImport.setValueAt(true, 2,2);
        } else {
            card = String.valueOf(++currentDisplayNum);
        }
        backButton.setEnabled(true);

        if(currentDisplayNum == 2) {
            System.out.println("Going to screen two...");
        } else if (currentDisplayNum == 3) {
            nextButton.setText("Finish");
            clearSummaryTable();
            System.out.println("Going to screen three...");

            for (int i=0; i<2; i++) {
                if (mergeCheckbox.isSelected() && i==0
                    && jTableImport.getValueAt(0,2).equals(true)
                    && jTableImport.getValueAt(1,2).equals(true)) {
                    //This is a hack -- only works with hard coded values
                    jTableSuccess.setValueAt(courses[0][0] + "-" + courses[0][1]
                                + ", " + courses[1][0] + "-" + courses[1][1], 0, 0);
                    i=2;
                } else {
                    if (jTableImport.getValueAt(i,2).equals(true)) {
                        jTableSuccess.setValueAt(courses[i][0] + "-" + courses[i][1], j, 0);
                        j++;
                    }
                }
            }
            if (jTableImport.getValueAt(2,2).equals(true)) {
                jTableUnsuccess.setValueAt(courses[2][0] + "-" + courses[2][1], 0, 0);
            }
        } else if (currentDisplayNum == 4) {
            dispose();
            resetWizard();
            System.out.println("Import complete.");
        }

        cl.show(mainPanel, card);
    }

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        String card = String.valueOf(--currentDisplayNum);
        if (currentDisplayNum == 1) {
            backButton.setEnabled(false);
            System.out.println("Going to screen one...");
        } else if (currentDisplayNum == 2) {
            nextButton.setText("Next");
            System.out.println("Going to screen two...");
        }

        cl.show(mainPanel, card);
    }

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
        dispose();
        resetWizard();
    }

    private void resetWizard() {
        currentDisplayNum = 1;
        nextButton.setText("Next");
        backButton.setEnabled(false);
        CardLayout cl = (CardLayout)(mainPanel.getLayout());
        String card = String.valueOf(currentDisplayNum);
        cl.show(mainPanel, card);
    }



    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Import dialog = new Import(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    private void initTable() {
        jTableImport = new JTable(new ImportModel());
        jTableImport.setRowSelectionAllowed(false);
        jTableImport.setColumnSelectionAllowed(false);
        jTableImport.setCellSelectionEnabled(false);
    }

    private void initSummaryTable() {
        jTableSuccess = new JTable(new SummaryModel());
        jTableSuccess.setRowSelectionAllowed(false);
        jTableSuccess.setColumnSelectionAllowed(false);
        jTableSuccess.setCellSelectionEnabled(false);

        jTableUnsuccess = new JTable(new SummaryModel());
        jTableUnsuccess.setRowSelectionAllowed(false);
        jTableUnsuccess.setColumnSelectionAllowed(false);
        jTableUnsuccess.setCellSelectionEnabled(false);
    }

    private void clearSummaryTable() {
        for (int i=0; i<2; i++) {
            jTableSuccess.setValueAt("", i, 0);
            jTableUnsuccess.setValueAt("", i, 0);
        }
    }

    
    class ImportModel extends AbstractTableModel {
        private String[] cols = {"Course", "Section", "Import"};
        private Object[][] data = {
            {courses[0][0], courses[0][1], new Boolean(false)},
            {courses[1][0], courses[1][1], new Boolean(false)},
            {courses[2][0], courses[2][1], new Boolean(false)},
        };
        
        public boolean isCellEditable(int row, int col) {
            return (col == 2) ? true : false;
        }

        public int getColumnCount() {
            return cols.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return cols[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }

}
    class SummaryModel extends AbstractTableModel {
        private String[] cols = {"Courses"};
        private Object[][] data = {{""}, {""}};

        public int getColumnCount() {
            return cols.length;
        }

        public int getRowCount() {
            return data.length;
        }

        public String getColumnName(int col) {
            return cols[col];
        }

        public Object getValueAt(int row, int col) {
            return data[row][col];
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public void setValueAt(Object value, int row, int col) {
            data[row][col] = value;
            fireTableCellUpdated(row, col);
        }
}


    // Variables declaration - do not modify
    private JButton nextButton, backButton, cancelButton;
    private JLabel continueLabel, importLabel, infoLabel,
            sucessLabel, unsuccessLabel, welcomeLabel;
    private JPanel mainPanel, welcomePanel, importPanel,
            summaryPanel;
    private JScrollPane importScrollPane;
    private JSeparator jSeparator1;
    private JCheckBox defaultCheckbox, mergeCheckbox;
    private JScrollPane successfulScrollPane, unsuccessfulScrollPane;
    private JTable jTableImport, jTableSuccess, jTableUnsuccess;
    private String courses[][];
    // End of variables declaration

}