/**
 * 
 */
package com.planimod.ui.views.employees;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.window.IShellProvider;

import com.patrikdufresne.managers.ManagedObject;
import com.patrikdufresne.managers.ManagerException;
import com.patrikdufresne.util.Localized;
import com.planimod.core.Employee;
import com.planimod.core.EmployeePreference;
import com.planimod.core.PlanimodManagers;
import com.planimod.core.TimeRanges;
import com.planimod.ui.PlanimodPolicy;
import com.planimod.ui.theme.Resources;
import com.planimod.ui.views.AbstractAddAction;

/**
 * Action to create a new Rider object and add it to the database.
 * 
 * @author dufresne
 * 
 */
public class AddEmployeeAction extends AbstractAddAction {
	/**
	 * Create a new action.
	 * 
	 * @param shellProvider
	 *            a shell provider in case the action need to display a message
	 *            box.
	 */
	public AddEmployeeAction(PlanimodManagers managers,
			IShellProvider shellProvider, ISelectionProvider selectionProvider) {
		super(managers, shellProvider, selectionProvider);
		setText(Localized
				.get(AddEmployeeAction.class, "AddEmployeeAction.text")); //$NON-NLS-1$
		setToolTipText(Localized.get(AddEmployeeAction.class,
				"AddEmployeeAction.tooltipText")); //$NON-NLS-1$
		setImageDescriptor(Resources
				.getImageDescriptor(Resources.ICON_LIST_ADD_16));
	}

	/**
	 * This implementation always return true.
	 * 
	 * @see net.ekwos.gymkhana.ui.views.AbstractAddAction#canCreateObject()
	 */
	@Override
	protected boolean canCreateObject() {
		return true;
	}

	/**
	 * This implementation create rider object.
	 * 
	 * @see net.ekwos.gymkhana.ui.views.AbstractAddAction#createObjects()
	 */
	@Override
	protected List<? extends ManagedObject> createObjects() {
		Employee e = new Employee();
		e.setLastname(Localized.get(AddEmployeeAction.class,
				"AddEmployeeAction.newEmployeeName")); //$NON-NLS-1$
		e.setHireDate(TimeRanges.removeSecond(new Date()));
		return Arrays.asList(e);
	}

	@Override
	public void selectObjects(Collection<? extends ManagedObject> list) {
		List<EmployeePreference> prefs;
		try {
			prefs = getManagers().getEmployeePreferenceManager()
					.listByEmployees((Collection<Employee>) list);
			if (prefs.size() > 0) {
				super.selectObjects(prefs);
			}
		} catch (ManagerException e) {
			PlanimodPolicy.showException(e);
		}
	}
}