package de.ugoe.cs.swe.exercises.exercisesheet;

import java.io.File;

import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import de.ugoe.cs.swe.exercises.exercise.ExerciseSVN;

public class ExerciseSheetPDFDeleteDialog extends TitleAreaDialog {

	private String lectureName;
	private Shell shell;
	private static String fileName;
	private static String fileNameSol;

	public String getLecture() {
		return lectureName;
	}

	public ExerciseSheetPDFDeleteDialog(Shell parentShell, String file,
			String solFile) {
		super(parentShell);
		super.setShellStyle(0);
		ExerciseSheetPDFDeleteDialog.fileName = file;
		ExerciseSheetPDFDeleteDialog.fileNameSol = solFile;
		this.shell = parentShell;
		shell.setEnabled(false);
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle("PDF deletion");
		setMessage("Removes the PDFs from the Repository.",
				IMessageProvider.INFORMATION);
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		parent.setLayout(layout);
		Label label1 = new Label(parent, SWT.NONE);
		label1.setText("Do you really want to delete the PDF?");

		GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gd.horizontalSpan = 1;

		return parent;

	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		((GridLayout) parent.getLayout()).numColumns = 2;

		GridData buttonlayout = new GridData(SWT.LEFT, SWT.BEGINNING, false,
				false);
		buttonlayout.minimumWidth = 200;
		Button button = new Button(parent, SWT.PUSH);
		button.setText("OK");
		button.setFont(JFaceResources.getDialogFont());
		button.setLayoutData(buttonlayout);
		button.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				File file = new File(ExerciseSVN.SVNpath + "/exercisesheets/"
						+ fileName.replaceAll(" ", "_") + ".pdf");
				File solFile = new File(ExerciseSVN.SVNpath
						+ "/exercisesheets/" + fileNameSol.replaceAll(" ", "_")
						+ ".pdf");

				if (file.exists())
					ExerciseSheet.delete(file);
				if (solFile.exists())
					ExerciseSheet.delete(solFile);
				shell.setEnabled(true);
				close();

			}
		});
		Button cancel = new Button(parent, SWT.PUSH);
		cancel.setText("Cancel");
		cancel.setFont(JFaceResources.getDialogFont());
		cancel.setLayoutData(buttonlayout);
		cancel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.setEnabled(true);
				close();
			}
		});
	}
}