/*
 * 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 removing one team from the employee preference.
 * 
 * @author Patrik Dufresne
 * 
 */
public class EmployeePreferredTeamRemoveAction extends Action {

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

	/**
	 * The shifts property name.
	 */
	public static final String SHIFTS = "shifts"; //$NON-NLS-1$

	/**
	 * The current employee preference.
	 */
	private EmployeePreference employeePreference;
	private PlanimodManagers managers;

	/**
	 * The current shifts.
	 */
	private List<Team> shifts;

	/**
	 * Create a new action.
	 * 
	 * @param managers
	 */
	public EmployeePreferredTeamRemoveAction(PlanimodManagers managers) {
		if (managers == null) {
			throw new NullPointerException();
		}
		this.managers = managers;

		setText(Localized.get(EmployeePreferredTeamRemoveAction.class,
				"EmployeeEditor.preferredTeamRemoveAction.text")); //$NON-NLS-1$
		setToolTipText(Localized.get(EmployeePreferredTeamRemoveAction.class,
				"EmployeeEditor.preferredTeamRemoveAction.toolTipText")); //$NON-NLS-1$
		setImageDescriptor(Resources
				.getImageDescriptor(Resources.ICON_LIST_REMOVE_16));
		setEnabled(canRun());
	}

	protected boolean canRun() {
		return this.employeePreference != null && this.shifts != null
				&& this.shifts.size() > 0;
	}

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

	/**
	 * Return the current shift list.
	 * 
	 * @return
	 */
	public List<Team> getShifts() {
		return this.shifts;
	}

	/**
	 * This implementation remove the current shifts from the preferred shift
	 * list.
	 */
	@Override
	public void run() {
		if (this.employeePreference == null
				|| this.employeePreference.getPreferredTeam() == null
				|| this.shifts == null) {
			return;
		}

		// Remove shifts
		List<Team> preferredTeams = new ArrayList<Team>(
				this.employeePreference.getPreferredTeam());
		preferredTeams.removeAll(this.shifts);

		this.employeePreference.setPreferredTeam(preferredTeams);

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

	}

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

	/**
	 * Sets the shifts list.
	 * 
	 * @param shifts
	 *            the shift list or null
	 */
	public void setShifts(List<Team> shifts) {
		firePropertyChange(SHIFTS, this.shifts, this.shifts = shifts);
		setEnabled(canRun());
	}
}
