package org.openlca.app.wizards.io;

import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.IExportWizard;
import org.eclipse.ui.IWorkbench;
import org.openlca.app.Messages;
import org.openlca.app.db.Database;
import org.openlca.core.database.ImpactMethodDao;
import org.openlca.core.database.ProcessDao;
import org.openlca.core.model.ImpactMethod;
import org.openlca.core.model.ModelType;
import org.openlca.core.model.Process;
import org.openlca.core.model.descriptors.BaseDescriptor;
import org.openlca.io.ecospold1.output.EcoSpold1Export;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Wizard for exporting processes and impact methods to the EcoSpold01 format
 */
public class EcoSpold01ExportWizard extends Wizard implements IExportWizard {

	private Logger log = LoggerFactory.getLogger(this.getClass());
	private ModelSelectionPage page;
	private final ModelType type;

	public EcoSpold01ExportWizard(ModelType type) {
		super();
		setNeedsProgressMonitor(true);
		this.type = type;
	}

	@Override
	public void addPages() {
		page = new ModelSelectionPage(type);
		addPage(page);
	}

	@Override
	public void init(final IWorkbench workbench,
			final IStructuredSelection selection) {
		setWindowTitle(Messages.ExportEcoSpold);
	}

	@Override
	public boolean performFinish() {
		boolean errorOccured = false;
		List<BaseDescriptor> models = page.getSelectedModels();
		try {
			getContainer().run(true, true, (monitor) -> {
				int size = models.size();
				monitor.beginTask(Messages.ExportingProcesses, size + 1);
				monitor.subTask(Messages.CreatingEcoSpoldFolder);
				EcoSpold1Export export = new EcoSpold1Export(
						page.getExportDestination());
				monitor.worked(1);
				doExport(models, monitor, export);
				monitor.done();
			});
		} catch (Exception e) {
			log.error("Perform finish failed", e);
			errorOccured = true;
		}
		return !errorOccured;
	}

	private void doExport(List<BaseDescriptor> models,
			IProgressMonitor monitor, EcoSpold1Export export)
			throws InterruptedException {
		ProcessDao pDao = new ProcessDao(Database.get());
		ImpactMethodDao mDao = new ImpactMethodDao(Database.get());
		try {
			for (BaseDescriptor d : models) {
				if (monitor.isCanceled())
					break;
				monitor.subTask(d.getName());
				if (type == ModelType.PROCESS) {
					Process process = pDao.getForId(d.getId());
					export.export(process);
				} else if (type == ModelType.IMPACT_METHOD) {
					ImpactMethod method = mDao.getForId(d.getId());
					export.export(method);
				}
				monitor.worked(1);
			}
		} catch (Exception e) {
			throw new InterruptedException(e.getMessage());
		}
	}
}

@@ -672,16 +672,69 @@
 Export;%0A
+import org.openlca.io.ecospold1.output.ExportConfig;%0A
 import o
@@ -1572,27 +1572,190 @@
 Models();%0A%09%09
-try
+ExportConfig config = new ExportConfig();%0A%09%09config.setSingleFile(true);%0A%09%09try (EcoSpold1Export export = new EcoSpold1Export(%0A%09%09%09%09page.getExportDestination(), config))
  %7B%0A%09%09%09getCon
@@ -1944,94 +1944,8 @@
 r);%0A
-%09%09%09%09EcoSpold1Export export = new EcoSpold1Export(%0A%09%09%09%09%09%09page.getExportDestination());%0A
 %09%09%09%09

use EcoSpold export configuration