package tmcsim.simulationmanager.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.xml.parsers.SAXParserFactory;

import tmcsim.client.cadclientgui.data.Incident;
import tmcsim.common.ScriptException;
import tmcsim.client.cadclientgui.ScriptHandler;
import tmcsim.common.SimulationException;
import tmcsim.common.CADEnums.SCRIPT_STATUS;
import tmcsim.simulationmanager.SimulationManagerView;
import tmcsim.simulationmanager.dialogs.AddIncidentDialog;


/**
 * ExportAction is an AbstractAction that exports Incident data from the 
 * current simulation.  (Initially, just shows data in popup window).
 * @author jdalbey
 */
@SuppressWarnings("serial")
public class ExportAction extends AbstractAction {
    
    /** Reference to the SimulationManagerView object. */
    private SimulationManagerView theSimManagerView = null;
    
    /** 
     * Constructor.
     * @param view View class object for the Simulation Manager.
     */     
    public ExportAction(SimulationManagerView view) {
        super("Export Incident Notes");
        theSimManagerView = view;
    }
    /** Perform the action of exporting. */
    public void actionPerformed(ActionEvent evt) 
    {
        /** Create a process that can run in a separate thread so the
         *  simulation can get back to work.
         */
        Runnable addRunnable = new Runnable() 
        {
            public void run() 
            {
                try {
                    // Retrieve the current incidents from the simulation mgr.
                    Vector<Incident> currIncidents = theSimManagerView.getModel().getIncidentList();
                    
                    StringBuffer sb = new StringBuffer();
                    // For each incident in the list
                    for (Incident inc : currIncidents) 
                    {
                        sb.append("Incident # " + inc.logNum + "\n");
                        // Retrieve the table of comments/notes the users created
                        DefaultTableModel notesTable = inc.getCommentsNotesTable();
                        // Retrieve the notes chronologically (Most recent is in first row)
                        for (int row=notesTable.getRowCount()-1; row >=0; row--)
                        {
                            // Combine the fields into one export entry
                            sb.append(notesTable.getValueAt(row,1) + " "); // time
                            String initials = (String) notesTable.getValueAt(row,2); // initials
                            // If there are no user intials, it's a scripted item
                            if (initials.length() == 0)
                            {
                                initials = "Script";
                            }
                            sb.append(initials + " ");
                            sb.append(notesTable.getValueAt(row,4) + "\n"); // notes
                        }
                        System.out.println(sb);
                    }
                    JOptionPane.showMessageDialog(theSimManagerView, sb, "Export", JOptionPane.INFORMATION_MESSAGE);
                } catch (SimulationException ex) {
                    Logger.getLogger(ExportAction.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };

        Thread theThread = new Thread(addRunnable);
            theThread.start();

    }

}
