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

package org.statcato.file;

import org.statcato.Statcato;
import org.statcato.LogWindow;
import org.statcato.DialogEdit;
import org.statcato.utils.HelperFunctions;

import javax.swing.JFileChooser;
import java.io.File;
import javax.swing.JOptionPane;
import java.util.Vector;
import java.util.Enumeration;

/**
 * A collection of file operations.
 * 
 * @author Margaret Yau
 * @version %I%, %G%
 * @since 1.0
 */
public class FileOperations {
    /**
     * Opens a log.
     *
     * @param app parent application
     */
    public static void openLogFile(Statcato app) {
        // create a file chooser
        JFileChooser fc = new JFileChooser();        
        fc.addChoosableFileFilter(new ExtensionFileFilter("HTML", "html"));
        fc.setAcceptAllFileFilterUsed(false);
        LogWindow LogTextPane = app.getLogTextPane();
        
        int returnValue = fc.showOpenDialog(app);
        
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //System.out.println("opened file:" + file.getName());
            
            String contents = HelperFunctions.getFileContents(file);
            // check if append to or overwrite current log window
            Object[] options = {"Overwrite", "append"};
            int choice = JOptionPane.showOptionDialog(app, 
                    "Would you like to overwrite the current log window with the file you are opening, or append file contents to current log window",
                    "Load Log Window", JOptionPane.YES_NO_OPTION, 
                    JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
            if (choice == 0) { // overwrite                
                app.compoundEdit = new DialogEdit("overwrite log");
                LogTextPane.addHeading("<br>Open saved log " +
                        "window (overwrite): " + file.getName());
                LogTextPane.overwrite(contents);
                app.compoundEdit.end();
                app.addCompoundEdit(app.compoundEdit);
            }
            else {// append                
                contents = "<br><hr><br>" + contents;
                app.compoundEdit = new DialogEdit("append log");
                LogTextPane.addHeading("<br>Open saved log window " +
                        "(append): " + file.getName());
                LogTextPane.addText(contents);
                app.compoundEdit.end();
                app.addCompoundEdit(app.compoundEdit);
            }          
        }            
    }

    /**
     * Opens a datasheet
     * 
     * @param app parent application
     */
    public static void openDatasheet(Statcato app) {
        // create a file chooser
        JFileChooser fc = new JFileChooser();                
        fc.addChoosableFileFilter(new ExtensionFileFilter("Excel (*.xls)", "xls"));
        fc.addChoosableFileFilter(new ExtensionFileFilter("Comma-separated values(*.csv)", "csv"));
        fc.addChoosableFileFilter(new ExtensionFileFilter("Tab-delimited values (*.txt)", "txt"));
        fc.setAcceptAllFileFilterUsed(false);
        
        int returnValue = fc.showOpenDialog(app);
        
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();    
            // excel
            if (FileChooserUtils.getExtension(file).equals("xls")) {
                Vector<String> sheets = HelperFunctions.readExcelFile(file);
                if (sheets != null) {
                    for (Enumeration e = sheets.elements(); e.hasMoreElements(); ) {
                        String str = (String) e.nextElement();
                        if (!str.trim().equals(""))
                            app.getDatasheetTabbedPane().addDatasheet(str, null);
                    }
                }
                else {
                    app.showErrorDialog("Error reading " +
                            "excel file.");
                    return;
                }                   
            }            
            else {           
                String contents = HelperFunctions.getFileContents(file);
                if (FileChooserUtils.getExtension(file).equals("csv")) {
                    contents = HelperFunctions.parseCSV(contents);
                }
                app.getDatasheetTabbedPane().addDatasheet(contents, file);
           }
        }               
    }

    /**
     * Opens a project.
     *
     * @param app parent application
     */
    public static void openProjectFile(Statcato app) {
        // create a file chooser
        JFileChooser fc = new JFileChooser();        
        fc.addChoosableFileFilter(new ExtensionFileFilter(
                "Statcato Project File (.stc)", Project.extension));
        fc.setAcceptAllFileFilterUsed(false);
        Project project = app.getProject();
        
        int returnValue = fc.showOpenDialog(app);
        
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            //System.out.println("opened file:" + file.getName());
            if (!project.readFile(file.getPath())) {
                app.showErrorDialog("Error opening project file");
            }
        }            
    }

}
