package com.planimod.ui.views.planif.printing;

import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.window.IShellProvider;

import com.patrikdufresne.managers.ManagerException;
import com.patrikdufresne.printing.IPrintFactory;
import com.patrikdufresne.printing.PrintAction;
import com.patrikdufresne.util.Localized;
import com.planimod.core.ApplicationSetting;
import com.planimod.core.GeneratePlanifContext;
import com.planimod.core.PlanimodManagers;
import com.planimod.ui.PlanimodPolicy;
import com.planimod.ui.preference.PlanimodPreferences;
import com.planimod.ui.printing.PlanimodPrintFactory;

/**
 * This action intend to print the planif.
 * 
 * @author Patrik Dufresne
 * 
 */
public class PlanifWeekSummaryPrintAction extends PrintAction {
	/**
	 * Define the preference key for this action
	 */
	private static final String PREF_KEY = "PlanifSummaryPrintAction.printPreference"; //$NON-NLS-1$

	/**
	 * Context to use.
	 */
	private GeneratePlanifContext context;

	/**
	 * Create a new print action.
	 * 
	 * @param context
	 * @param shell
	 * @param operation
	 * @param runnableContext
	 */
	public PlanifWeekSummaryPrintAction(GeneratePlanifContext context,
			IShellProvider shell, int operation,
			IRunnableContext runnableContext) {
		super(shell, new PlanifWeekSummaryPrintFactory(), operation,
				runnableContext);
		if (context == null) {
			throw new NullPointerException();
		}
		this.context = context;
		setPrefKey(PREF_KEY);
		setPreferenceStore(PlanimodPreferences.getPreferenceStore());
		setEnabled(canRun());
		if (operation == PRINT_ACTION) {
			setText(Localized.get(PlanifWeekSummaryPrintAction.class,
					"PlanifWeekSummaryPrintAction.print")); //$NON-NLS-1$
		} else {
			setText(Localized.get(PlanifWeekSummaryPrintAction.class,
					"PlanifWeekSummaryPrintAction.preview")); //$NON-NLS-1$
		}
	}

	/**
	 * Returns True if the action can be run.
	 * 
	 * @return
	 */
	protected boolean canRun() {
		return this.context != null && !this.context.isDisposed();
	}
	
	/**
	 * Returns the managers used by this action.
	 * 
	 * @return
	 */
	protected PlanimodManagers getManagers() {
		return this.context.getManagers();
	}

	/**
	 * This implementation set the employee to print
	 * 
	 * @param factory
	 *            the factory
	 */
	@Override
	protected void initFactory(IPrintFactory factory) {
		// Get date from state
		if (this.context == null) {
			return;
		}

		// Get matching event for the time range.
		try {
			// Sets subtitle
			String custonerName = getManagers().getApplicationSettingManager()
					.getByName(ApplicationSetting.CUSTOMER_NAME);
			((PlanimodPrintFactory) factory).setSubTitle(custonerName);
			
			((PlanifWeekSummaryPrintFactory) factory).setTasks(this.context
					.listTask());

			((PlanifWeekSummaryPrintFactory) factory)
					.setEmployeePreferences(this.context
							.listEmployeePreferences());

			((PlanifWeekSummaryPrintFactory) factory)
					.setNonAvailabilties(this.context
							.getManagers()
							.getNonAvailabilityManager()
							.list(this.context.getStart(),
									this.context.getEnd()));

		} catch (ManagerException e) {
			PlanimodPolicy.showException(e);
			return;
		}
	}

	@Override
	public void run() {
		
		// Prompt the user for the data to be compare
		
		
		super.run();
	}

}
