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.jface.viewers.TableViewerColumn;
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;
import de.ugoe.cs.swe.exercises.lecturer.LecturerContentProvider;
import de.ugoe.cs.swe.exercises.lecturer.LecturerLabelProvider;

public class LectureEventLecturerAddDialog extends TitleAreaDialog {


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


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

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

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

	@Override
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setTitle("Add a new lecture Event");
		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 LecturerContentProvider());
		viewer.setLabelProvider(new LecturerLabelProvider());
		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) {
		String[] titles = { "Title", "First Name", "Last Name" };
		int[] bounds = {200, 200, 200 };

		for (int i = 0; i < titles.length; i++) {
			TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
			column.getColumn().setText(titles[i]);
			column.getColumn().setWidth(bounds[i]);
			Table table = viewer.getTable();
			table.setHeaderVisible(false);
			table.setLinesVisible(true);
		}
	}
}