/*
 * Copyright (c) 2011, Patrik Dufresne. All rights reserved.
 * Patrik Dufresne PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.planimod.ui.databinding.manager;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;

import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.value.IObservableValue;

import com.patrikdufresne.managers.ManagerException;
import com.patrikdufresne.managers.databinding.ManagedObjectComputedSet;
import com.planimod.core.TimeRanges;
import com.planimod.core.Employee;
import com.planimod.core.NonAvailabilityManager;
import com.planimod.core.Task;
import com.planimod.core.TaskManager;
import com.planimod.core.PlanimodManagers;

/**
 * This computed set is used to wrap the function
 * {@link NonAvailabilityManager#listProductionEvents(Planif, Date, Date)} and
 * make it observable.
 * 
 * @param managers
 *            the managers to use
 * @param planif
 *            the planif observable
 * @param start
 *            the start date observable
 * @param end
 *            the end date observable
 * @return the observable set
 */
public class TasksByEmployeeComputedSet extends ManagedObjectComputedSet {

	/**
	 * The observable employee value.
	 */
	private final IObservableValue employee;
	private final IObservableValue end;
	private final IObservableValue start;

	/**
	 * Create an observable set.
	 * 
	 * @param managers
	 *            the managers to use
	 * @param planif
	 *            the planif observable
	 * @param start
	 *            the start date observable
	 * @param end
	 *            the end date observable
	 * @return the observable set
	 */
	public TasksByEmployeeComputedSet(PlanimodManagers managers,
			IObservableValue start, IObservableValue end,
			IObservableValue employee) {
		super(managers, Task.class, new IObservable[] { start, end, employee });
		this.start = start;
		this.end = end;
		this.employee = employee;
	}

	/**
	 * This implementation run the
	 * {@link TaskManager#listByEmployee(Planif, Date, Date, Employee)}
	 * function.
	 */
	@Override
	protected Collection doList() throws ManagerException {
		// Get the current planif value from the planif state.
		if (getStart() == null || getEnd() == null || getEmployee() == null) {
			return Collections.EMPTY_LIST;
		}
		return ((PlanimodManagers) getManagers()).getTaskManager()
				.listByEmployee(getStart(), getEnd(), getEmployee());
	}

	/**
	 * Selector to filter according to the listByEmployee.
	 */
	@Override
	protected boolean doSelect(Object element) {
		// Get the current planif value from the planif state.
		Date start = getStart();
		Date end = getEnd();
		if (start == null || end == null) {
			return false;
		}
		return element instanceof Task
				&& TimeRanges.intersectEquals(((Task) element), start, end);
	}

	protected Employee getEmployee() {
		if (this.employee.getValue() instanceof Employee) {
			return (Employee) this.employee.getValue();
		}
		return null;
	}

	/**
	 * Returns the start date value.
	 * 
	 * @return the start date or null.
	 */
	protected Date getEnd() {
		if (this.end.getValue() instanceof Date) {
			return (Date) this.end.getValue();
		}
		return null;
	}

	/**
	 * Returns the end date value.
	 * 
	 * @return the end date or null.
	 */
	protected Date getStart() {
		if (this.start.getValue() instanceof Date) {
			return (Date) this.start.getValue();
		}
		return null;
	}
}