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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.action.Action;

import com.patrikdufresne.managers.ManagerException;
import com.patrikdufresne.util.Localized;
import com.planimod.core.EmployeePreference;
import com.planimod.core.PlanimodManagers;
import com.planimod.core.Team;
import com.planimod.ui.PlanimodPolicy;
import com.planimod.ui.theme.Resources;

/**
 * This action is creating all the teams to fill the employee preferences.
 * 
 * @author Patrik Dufresne
 * 
 */
public class EmployeePreferredTeamAddAllAction extends Action {

	/**
	 * The employee preference property.
	 */
	public static final String EMPLOYEE_PREFERENCE = "employeePreference"; //$NON-NLS-1$

	/**
	 * The employee preference.
	 */
	private EmployeePreference employeePreference;
	/**
	 * Managers to use.
	 */
	private PlanimodManagers managers;

	/**
	 * Create a new action.
	 * 
	 * @param direction
	 *            the direction of the move
	 */
	public EmployeePreferredTeamAddAllAction(PlanimodManagers managers) {
		if (managers == null) {
			throw new IllegalArgumentException();
		}
		this.managers = managers;
		setText(Localized.get(EmployeePreferredTeamAddAllAction.class,
				"EmployeeEditor.preferredTeamAddAllAction.text")); //$NON-NLS-1$
		setToolTipText(Localized.get(EmployeePreferredTeamAddAllAction.class,
				"EmployeeEditor.preferredTeamAddAllAction.toolTipText")); //$NON-NLS-1$
		setImageDescriptor(Resources
				.getImageDescriptor(Resources.ICON_LIST_ADD_16));
		setEnabled(canRun());
	}

	protected boolean canRun() {
		return this.employeePreference != null;
	}

	/**
	 * Returns the current employee preference.
	 * 
	 * @return
	 */
	public EmployeePreference getEmployeePreference() {
		return this.employeePreference;
	}

	@Override
	public void run() {
		if (this.employeePreference == null) {
			return;
		}

		// Retrieve the current preferred shift list.
		List<Team> preferredTeams = this.employeePreference.getPreferredTeam();
		if (preferredTeams == null) {
			preferredTeams = new ArrayList<Team>();
		} else {
			preferredTeams = new ArrayList<Team>(preferredTeams);
		}

		// Add any missing shift
		List<Team> allShifts;
		try {
			allShifts = this.managers.getTeamManager().list();
		} catch (ManagerException e) {
			PlanimodPolicy.showException(e);
			return;
		}
		for (Team s : allShifts) {
			if (!preferredTeams.contains(s)) {
				preferredTeams.add(s);
			}
		}
		this.employeePreference.setPreferredTeam(preferredTeams);

		// Persist the modification
		try {
			managers.updateAll(Arrays.asList(this.employeePreference));
		} catch (ManagerException e) {
			PlanimodPolicy.showException(e);
		}
	}

	/**
	 * Sets the employee preference to update.
	 * 
	 * @param employeePreference
	 */
	public void setEmployeePreference(EmployeePreference employeePreference) {
		firePropertyChange(EMPLOYEE_PREFERENCE, this.employeePreference,
				this.employeePreference = employeePreference);
		setEnabled(canRun());
	}

}
