package de.afbb.bibo.ui.form;

import java.util.Collection;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;

import de.afbb.bibo.share.model.Copy;
import de.afbb.bibo.ui.BiboFormToolkit;

/**
 * displays statistic information
 *
 * @author David Becker
 *
 */
public class StatisticForm extends Composite {

	private final BiboFormToolkit toolkit;
	private Text txtCount;
	private Text txtMedian;
	private Text txtMax;

	private final Date today = new Date();

	public StatisticForm(final Composite parent) {
		super(parent, SWT.NONE);
		toolkit = new BiboFormToolkit(Display.getCurrent());
		createUi();
	}

	private void createUi() {
		final GridLayout outerLayout = new GridLayout(1, false);
		outerLayout.marginHeight = outerLayout.marginWidth = 0;
		setLayout(outerLayout);

		final Composite content = toolkit.createComposite(this);
		final GridLayout layout = new GridLayout(2, false);
		layout.marginHeight = layout.marginWidth = 0;
		content.setLayout(layout);

		content.setEnabled(false);

		toolkit.createLabel(content, "Anzahl Medien");
		txtCount = toolkit.createText(content, "0", SWT.RIGHT | SWT.READ_ONLY | SWT.BORDER);

		toolkit.createLabel(content, "Ausleihtage Durchschnitt");
		txtMedian = toolkit.createText(content, "0", SWT.RIGHT | SWT.READ_ONLY | SWT.BORDER);

		toolkit.createLabel(content, "Ausleihtage Maximum");
		txtMax = toolkit.createText(content, "0", SWT.RIGHT | SWT.READ_ONLY | SWT.BORDER);

		GridDataFactory.fillDefaults().hint(50, SWT.DEFAULT).applyTo(txtCount);
		GridDataFactory.fillDefaults().hint(50, SWT.DEFAULT).applyTo(txtMedian);
		GridDataFactory.fillDefaults().hint(50, SWT.DEFAULT).applyTo(txtMax);
	}

	public void setInput(final Collection<Copy> input) {
		int count = 0;
		double median = 0.0f;
		long max = 0;
		if (input != null) {
			count = input.size();

			long maxDeltaDays = 0;
			long totalDeltaDays = 0;
			double countDeltas = 0.0;

			for (final Copy copy : input) {
				if (copy != null) {
					// year 2038 problem, but who cares?
					final long deltaDays = TimeUnit.MILLISECONDS
							.toDays(today.getTime() - copy.getBorrowDate().getTime());
					if (maxDeltaDays < deltaDays) {
						maxDeltaDays = deltaDays;
					}
					totalDeltaDays += deltaDays;
					countDeltas += 1.0;
				}
			}
			if (countDeltas > 0.0) {
				median = totalDeltaDays / countDeltas;
			}
			max = maxDeltaDays;
		}
		txtCount.setText(String.valueOf(count));
		txtMedian.setText(String.valueOf(median));
		txtMax.setText(String.valueOf(max));
	}

}

@@ -24,16 +24,45 @@
 .form;%0A%0A
+import java.text.DateFormat;%0A
 import j
@@ -692,16 +692,54 @@
  txtMax;
+%0A%09private Text txtOldestInventoryDate;
 %0A%0A%09priva
@@ -1740,16 +1740,177 @@
 RDER);%0A%0A
+%09%09toolkit.createLabel(content, %22%C3%84ltestes Inventar-Datum%22);%0A%09%09txtOldestInventoryDate = toolkit.createText(content, %22%22, SWT.CENTER %7C SWT.READ_ONLY %7C SWT.BORDER);%0A%0A
 %09%09GridDa
@@ -1931,33 +1931,33 @@
 Defaults().hint(
-5
+8
 0, SWT.DEFAULT).
@@ -2005,33 +2005,33 @@
 Defaults().hint(
-5
+8
 0, SWT.DEFAULT).
@@ -2092,9 +2092,9 @@
 int(
-5
+8
 0, S
@@ -2122,16 +2122,104 @@
 xtMax);%0A
+%09%09GridDataFactory.fillDefaults().hint(80, SWT.DEFAULT).applyTo(txtOldestInventoryDate);%0A
 %09%7D%0A%0A%09pub
@@ -2325,16 +2325,45 @@
 ax = 0;%0A
+%09%09Date inventoryDate = null;%0A
 %09%09if (in
@@ -2838,16 +2838,161 @@
  += 1.0;
+%0A%0A%09%09%09%09%09if (inventoryDate == null %7C%7C inventoryDate.compareTo(copy.getInventoryDate()) %3E 0) %7B%0A%09%09%09%09%09%09inventoryDate = copy.getInventoryDate();%0A%09%09%09%09%09%7D
 %0A%09%09%09%09%7D%0A%09
@@ -3230,9 +3230,122 @@
 );%0A%09
-%7D%0A%0A
+%09txtOldestInventoryDate.setText(inventoryDate != null ? DateFormat.getDateInstance().format(inventoryDate) : %22%22);%0A%09
 %7D%0A
+%7D

add field for oldest inventory date to statistic form