/*
 *  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 com.approximatrix.draco.application.ApplicationManager;
import com.approximatrix.draco.main.ui.MainWindow;
import com.approximatrix.draco.io.XMLio.SaveWorksheet;
import com.approximatrix.draco.io.sql.DracoSQLWrite;
import com.approximatrix.draco.io.ui.IOFileWorkerDialog;
import com.approximatrix.draco.io.ui.IOWorker;

public class SaveEASY implements IOWorker, DracoSaver {

	private static final String SAVE_WORKSHEET_TITLE = "Save "+ApplicationManager.APP_NAME+" File...";
	private static final String[] SAVE_STEPS = {"Generating OpenDocument File",
												"Writing to Disk"};
	private static final int TOTAL_STEPS = 2;
	
	private int current_step = 0;
	private String filename = null;
	private MainWindow parent = null;
	
	private SaveWorksheet sw = null;
	private boolean success = true;
	
	private boolean quicksave_only = false;
	private boolean allow_quicksave = true;
	
	public SaveEASY(String filename, MainWindow parent) {
		this.filename = filename;
		current_step = 1;
		this.parent = parent;
	}
	
	public void save() {
		new IOFileWorkerDialog(this).setVisible(true);
	}
	
	public String getWorkerTitle() {
		return SAVE_WORKSHEET_TITLE;
	}

	public void doStep() {

		switch(current_step) {
			case 1:
				sw = new SaveWorksheet(filename);
				success = (sw != null);
				break;
			case 2:
				sw.setQuicksaveOnly(quicksave_only);
				sw.setAllowQuicksave(allow_quicksave);
				success = sw.saveData(parent);
				break;
			default:
				success = false;
		}
		current_step++;
	}

	public int getStepNumber() {
		return current_step;
	}

	public int getTotalSteps() {
		return TOTAL_STEPS;
	}

	public String getStatus() {
		return SAVE_STEPS[current_step-1];
	}
	
	public JFrame getParentWindow() {
		return parent;
	}
	
	public boolean stepSuccess() {
		return success;
	}
	
	public String getError() {
		if(sw != null) 
			return sw.getError();
		else
			return null;
	}
	
	public void setQuicksaveOnly(boolean qso) {
		quicksave_only = qso;
	}
	
	public void setAllowQuicksave(boolean allow) {
		allow_quicksave = allow;
	}
}
