/*
 * 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.Arrays;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.window.IShellProvider;
import org.eclipse.jface.window.Window;

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

/**
 * Action to edit the preferred position of an employee. This action open a new
 * dialog to edit the preferred position.
 * 
 * @author Patrik Dufresne
 * 
 */
public class EmployeeEditPreferredPositionAction 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;

	private IShellProvider shell;

	/**
	 * Create a new action.
	 * 
	 * @param direction
	 *            the direction of the move
	 */
	public EmployeeEditPreferredPositionAction(PlanimodManagers managers,
			IShellProvider shell) {
		if (managers == null || shell == null) {
			throw new IllegalArgumentException();
		}
		this.managers = managers;
		this.shell = shell;
		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() {
		// Check if the observable is a employee-preference
		if (this.employeePreference == null) {
			return;
		}

		// Open the dialog
		PreferredPositionDialog dlg = new PreferredPositionDialog(
				this.shell.getShell(), this.managers);
		dlg.setShift(this.employeePreference.getPreferredPositionTeam());
		dlg.setPosition(this.employeePreference.getPreferredPosition());

		switch (dlg.open()) {
		case Window.OK:
			// Update the entity
			this.employeePreference.setPreferredPosition(dlg.getPosition());
			this.employeePreference.setPreferredPositionTeam(dlg.getShift());
			break;
		case PreferredPositionDialog.CLEAR_ID:
			// Update the entity
			this.employeePreference.setPreferredPosition(null);
			this.employeePreference.setPreferredPositionTeam(null);
			break;
		default:
			// Operation canceled by user
			return;
		}

		// Persists the modification
		try {
			this.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());
	}

}
