package com.interpress_project.modernshare.client.view.wizard;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.wizard.Wizard;

import com.interpress_project.modernshare.client.controller.command.ISVNCommand;
import com.interpress_project.modernshare.client.controller.command.SVNCommandFactory;
import com.interpress_project.modernshare.client.controller.command.SVNCommandManager;
import com.interpress_project.modernshare.client.controller.command.exceptions.CommandException;
import com.interpress_project.modernshare.client.controller.model.ISVNFileItem;
import com.interpress_project.modernshare.client.events.LogEvent;
import com.interpress_project.modernshare.client.events.LogEventSource;
import com.interpress_project.modernshare.client.events.LogEventType;
import com.interpress_project.modernshare.client.view.ViewManager;

public class LocalExportWizard extends Wizard {
	private final ViewManager vmgr = ViewManager.getInstance();
	private final LogEventSource logEventSource = LogEventSource.getInstance();
	private ExportWelcomePage exportWelcomePage = new ExportWelcomePage();
	private ExportWizardPage1 exportWizardPage1 = null;
	private ExportWizardPage2 exportWizardPage2 = new ExportWizardPage2();
	private ExportConfirmPage exportConfirmPage = new ExportConfirmPage();

	/**
	 * LocalExportWizard
	 * @param revlist
	 */
	public LocalExportWizard(ArrayList<ISVNFileItem> revlist) {
		addPage(exportWelcomePage);
		exportWizardPage1 = new ExportWizardPage1(revlist);
		addPage(exportWizardPage1);
		addPage(exportWizardPage2);
		addPage(exportConfirmPage);

		setNeedsProgressMonitor(true);
		setWindowTitle("ホットショット・フォルダ - 取り出しウィザード");
	}

	/**
	 * performFinish
	 */
	@Override
	public boolean performFinish() {
		/**
		 * スレッドの定義
		 */
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InterruptedException, InvocationTargetException {
				export(monitor);
			}
		};
		/**
		 * 上記スレッドの実行
		 */
		try {
			//			logEventSource.addEventListener(ViewManager.getInstance());
			getContainer().run(true, true, op);
		}
		catch (InvocationTargetException ex) {
			logEventSource.fireEvent(new LogEvent(this, ex.getLocalizedMessage(), LogEventType.ERROR, ex));
			vmgr.getDialogManager().createErrorMessageDialog(ex.getMessage());
			return false;
		}
		catch (InterruptedException ex) {
		}
		finally {
			//			logEventSource.removeEventListener(ViewManager.getInstance());
		}
		return true;
	}

	/**
	 * export
	 * @param monitor
	 * @throws InvocationTargetException
	 */
	private void export(IProgressMonitor monitor) throws InvocationTargetException {
		ISVNFileItem item = exportWizardPage1.getISVNFileItem();
		String path = exportWizardPage2.getLocalFileName();

		try {
			monitor.beginTask("データの取り出しを開始します...", 1);

			SVNCommandFactory factory = SVNCommandFactory.getInstance();
			ISVNCommand command = factory.createLocalExportCommand(item, path);
			SVNCommandManager cmdmgr = SVNCommandManager.getInstance();
			cmdmgr.execCommand(command);

			monitor.worked(1);
		}
		catch (CommandException ex) {
			ex.printStackTrace();
		}
		finally {
			monitor.done();
		}
	}
}
