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


import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
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.Shell;
import org.eclipse.swt.widgets.Table;

import de.ugoe.cs.swe.exercises.lecturer.Lecturer;


public class LectureEventLecturerDeleteDialog extends TitleAreaDialog {


	private int[] lectureEvent = new int[3];
	int[] lecturesListId;
	private Lecturer[] lecturerList;
	private TableViewer viewer;
	private int lectureEventId;

	public void setLectureEventId(int id) {
		lectureEventId = id;
	}

	public Lecturer[] getLecturerList() {
		return lecturerList;
	}

	public int[] getLectureEvent() {
		return lectureEvent;
	}

	public LectureEventLecturerDeleteDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle("Delete a lecturer");
		setMessage("Please input all the data for the new lecture Event",
				IMessageProvider.INFORMATION);
		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		parent.setLayout(layout);

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

		 viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL);
		CreateLecturerColumns(viewer);
		viewer.setContentProvider(new LectureEventLecturerContentProvider(LectureEvent.get(lectureEventId)));
		viewer.setLabelProvider(new LectureEventLecturerLabelProvider());
		viewer.setInput(getShell());

		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 2;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		viewer.getControl().setLayoutData(gridData);

		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) {

					StructuredSelection sel = ((StructuredSelection) viewer
							.getSelection());


					lecturerList = new Lecturer[sel.size()];
					Object[] fuckdisshit = sel.toArray();
					for ( int i=0 ; i<fuckdisshit.length ; i++) {
					lecturerList[i] = (Lecturer) fuckdisshit[i];
					}

					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) {
				close();
			}
		});
	}

	private void CreateLecturerColumns(TableViewer viewer) {

			Table table = viewer.getTable();
			table.setHeaderVisible(false);
			table.setLinesVisible(true);
		}
	}
