package edu.oregonstate.cope.eclipse.installer;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;

import edu.oregonstate.cope.clientRecorder.Uninstaller;
import edu.oregonstate.cope.eclipse.ui.handlers.SurveyWizard;

/**
 * Runs plugin installation mode. This is implemented by storing files both in
 * the eclipse installation path and in the workspace path. When either one does
 * not exist, it is copied there from the other place. When none exists, the one
 * time installation code is run.
 * 
 */
public class Installer {

	private static final String SURVEY_FILENAME = "survey.txt";

	private Path workspaceDirectory;
	private Path permanentDirectory;
	private Uninstaller uninstaller;
	private String installationConfigFileName;

	private class SurveyOperation extends InstallerOperation {

		public SurveyOperation(Path workspaceDirectory, Path permanentDirectory) {
			super(workspaceDirectory, permanentDirectory);
		}

		@Override
		protected void doNoFileExists(File workspaceFile, File permanentFile) throws IOException {
			System.out.println("GIVE SURVEY");

			SurveyWizard sw = new SurveyWizard();
			WizardDialog wizardDialog = new WizardDialog(Display.getDefault().getActiveShell(), sw);
			wizardDialog.open();

			writeContentsToFile(workspaceFile.toPath(), sw.getSurveyResults());
			writeContentsToFile(permanentFile.toPath(), sw.getSurveyResults());
		}
	}

	private class ConfigInstallOperation extends InstallerOperation {

		public ConfigInstallOperation(Path workspaceDirectory,
				Path permanentDirectory) {
			super(workspaceDirectory, permanentDirectory);
		}

		@Override
		protected void doNoFileExists(File workspaceFile, File permanentFile) throws IOException {
			uninstaller.initUninstallInMonths(3);

			Files.copy(permanentFile.toPath(), workspaceFile.toPath());
		}

	}

	public Installer(Path workspaceDirectory, Path permanentDirectory,
			Uninstaller uninstaller, String installationConfigFileName) {
		this.workspaceDirectory = workspaceDirectory;
		this.permanentDirectory = permanentDirectory;
		this.uninstaller = uninstaller;
		this.installationConfigFileName = installationConfigFileName;

		System.err.println(workspaceDirectory);
		System.err.println(permanentDirectory);
	}

	public void doInstall() {
		try {
			new SurveyOperation(workspaceDirectory, permanentDirectory).perform(SURVEY_FILENAME);
			new ConfigInstallOperation(workspaceDirectory, permanentDirectory).perform(installationConfigFileName);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

@@ -726,16 +726,75 @@
 ey.txt%22;
+%0A%09private static final String EMAIL_FILENAME = %22email.txt%22;
 %0A%0A%09priva
@@ -987,32 +987,33 @@
 lerOperation %7B%0A%0A
+%0A
 %09%09public SurveyO
@@ -1572,20 +1572,108 @@
 lts());%0A
+%09%09%09%0A%09%09%09writeContentsToFile(permanentDirectory.resolve(EMAIL_FILENAME), sw.getEmail());%0A
 %09%09%7D%0A
+%0A
 %09%7D%0A%0A%09pri
@@ -2092,24 +2092,349 @@
 ());%0A%09%09%7D%0A%0A%09%7D
+%0A%09%0A%09private class EmailInstallOperation extends InstallerOperation%7B%0A%0A%09%09public EmailInstallOperation(Path workspaceDirectory,%0A%09%09%09%09Path permanentDirectory) %7B%0A%09%09%09super(workspaceDirectory, permanentDirectory);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09protected void doNoFileExists(File workspaceFile, File permanentFile) throws IOException %7B%0A%09%09%7D%0A%09%09%0A%09%7D
 %0A%0A%09public In
@@ -2963,16 +2963,16 @@
 ENAME);%0A
-
 %09%09%09new C
@@ -3069,16 +3069,110 @@
 leName);
+%0A%09%09%09new EmailInstallOperation(workspaceDirectory, permanentDirectory).perform(EMAIL_FILENAME);
 %0A%0A%09%09%7D ca

the email is persisted