/*
 *  Draco Econometrics
 *  Copyright (C) 2006-2007 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 javax.swing.JFrame;
import javax.swing.SwingUtilities;


import com.approximatrix.draco.application.ApplicationManager;
import com.approximatrix.draco.main.ui.MainWindow;
import com.approximatrix.draco.datamodel.StatTableModel;
import com.approximatrix.draco.io.XMLio.LoadWorksheet;
import com.approximatrix.draco.io.ui.IOFileWorkerDialog;
import com.approximatrix.draco.io.ui.IOWorker;

public class LoadEASY implements IOWorker, DracoLoader {

	private static final String LOAD_WORKSHEET_TITLE = "Loading "+ApplicationManager.APP_NAME+" File...";
	private static final String[] LOAD_STEPS = {"Accessing OpenDocument File",
												"Loading into Memory",
												"Confirming "+ApplicationManager.APP_NAME+" Document",
												"Parsing and Loading Data",
												"Reconstructing Analyses",
												"Generating Text Windows",
												"Rebuilding Charts"};
	private static final int TOTAL_STEPS = 7;
	
	private int current_step = 0;
	private String filename = null;
	private MainWindow parent = null;
	
	private LoadWorksheet lw = null;
	private boolean success = true;
	
	public LoadEASY(String filename, MainWindow parent) {
		this.filename = filename;
		current_step = 1;
		this.parent = parent;
	}
	
	public void load() {
		parent.setUpdatableTable(false);
		parent.setVisibleDesktop(false);
		new IOFileWorkerDialog(this).setVisible(true);
		
		// Set the table to viewable again
		Runnable resetUpdateable = new Runnable() {
            public void run() {
            	parent.setUpdatableTable(true);
            	parent.setVisibleDesktop(true);
            }
        };
        SwingUtilities.invokeLater(resetUpdateable);
		
	}

	public StatTableModel getLoadedData() {
		if(lw != null)
			return lw.getLoadedModel();
		else
			return null;
	}
	
	public String getWorkerTitle() {
		return LOAD_WORKSHEET_TITLE;
	}

	public void doStep() {
		boolean loaded = true;

		switch(current_step) {
			case 1:
				lw = new LoadWorksheet(filename,parent);
				success = (lw != null);
				break;
			case 2:
				lw.allowODSCharts(false);
				success = lw.openIntoMemory();
				break;
			case 3:
				success = lw.createdByEasy();
				break;
			case 4:
				if(success) {
					parent.closeAllChildren();
					success = lw.load();
				}
				break;
			case 5:
				if(success)
					success = lw.buildAnalyses();
				success = true;
				break;
			case 6:
				if(success)
					success = lw.buildTexts();
				success = true;
				break;
			case 7:
				if(success)
					success = lw.buildCharts();
				success = true;
				break;
			default:
				success = false;
		}
		current_step++;
		
		// Not necessary
		//parent.updateWindowNames();
	}

	public int getStepNumber() {
		return current_step;
	}

	public int getTotalSteps() {
		return TOTAL_STEPS;
	}

	public String getStatus() {
		return LOAD_STEPS[current_step-1];
	}
	
	public JFrame getParentWindow() {
		return parent;
	}
	
	public boolean stepSuccess() {
		return success;
	}

	public String getError() {
		if(lw != null) 
			return lw.getError();
		else
			return null;
	}
}
