/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2008 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.io;

import java.awt.Component;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import com.approximatrix.draco.application.ApplicationManager;
import com.approximatrix.draco.application.ChildFrameRegistrar;
import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.io.filters.EASYFilter;
import com.approximatrix.draco.io.filters.SQLFilter;
import com.approximatrix.draco.main.ui.MainWindow;

public class GeneralLoadWorksheet {

	public static boolean LoadWorksheet(MainWindow parent) {
		JFileChooser chooser = new JFileChooser();
		//chooser.addChoosableFileFilter(new ODSFilter());
		chooser.addChoosableFileFilter(new EASYFilter());
		chooser.addChoosableFileFilter(new SQLFilter());
		
		if(parent != null) {
			if(parent.getCurrentFilename() != null)
				chooser.setCurrentDirectory(new java.io.File(parent.getCurrentFilename()));
		}
		
	    chooser.setDialogTitle("Choose File to Load...");
	    if (chooser.showOpenDialog(chooser) == JFileChooser.APPROVE_OPTION) { 
	    	String filename = chooser.getSelectedFile().toString();
	    	
	    	return LoadFile(filename, parent, true);
	    			    		
	    } else
	    	return false;
	}
	
	public static boolean LoadFile(String filename, final MainWindow parent, boolean closeEmtpyOriginal) {
		if(filename == null) return false;
		
		DracoLoader loader = null;
    	
    	MainWindow mw = ApplicationManager.RequestAndReturnNewWindow();
    	
    	if(new EASYFilter().accept(new File(filename)))
    		loader = new LoadEASY(filename,mw);
    	else
    		loader = new LoadDracoSQL(filename,mw);
    	
    	loader.load();
    	StatTableModel data = null;
    	
    	if(loader.stepSuccess())
    		data = loader.getLoadedData();
    
    	if(data != null) {
    		mw.setWindowTableModel(loader.getLoadedData(),filename);
    		
    		// Close the parent window if appropriate
    		if(parent != null && closeEmtpyOriginal) {
    			if(parent.isInitialWindow()) {
    				SwingUtilities.invokeLater(new Runnable() {
    					public void run() {
    						ApplicationManager.CloseWindow(parent);
    						parent.dispose();
    					}
    				});
    			}
    		}
    		return true;
    	} else {
    		final ChildFrameRegistrar newwindow = mw;
    		SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					ApplicationManager.CloseWindow(newwindow);
					parent.dispose();
				}
			});
    		if(parent != null) {
    			if(loader.getError() != null)
    				JOptionPane.showMessageDialog(parent,loader.getError(),ApplicationManager.APP_NAME+" Error",JOptionPane.ERROR_MESSAGE);
    			else
    				JOptionPane.showMessageDialog(parent,"Problem loading file...",ApplicationManager.APP_NAME+" Error",JOptionPane.ERROR_MESSAGE);
    		}
    		return false;
    	}
    	
	}

	private volatile boolean running = false;
	private String filename = null;
	
	public GeneralLoadWorksheet(String filename) {
		this.filename = filename;
		this.running = true;
	}
	
	public void load() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				LoadFile(filename, null, false);
				GeneralLoadWorksheet.this.running = false;
			}
		});
		
	}
	
	public boolean isLoading() {
		return this.running;
	}
	
}
