package com.planimod.ui.views.employees;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import net.sf.paperclips.ColumnPrint;
import net.sf.paperclips.DefaultGridLook;
import net.sf.paperclips.GridPrint;
import net.sf.paperclips.LineBorder;
import net.sf.paperclips.Print;
import net.sf.paperclips.TextPrint;

import com.patrikdufresne.printing.FontDataUtil;
import com.patrikdufresne.util.Localized;
import com.planimod.core.EmployeePreference;
import com.planimod.core.comparators.EmployeePreferenceComparators;
import com.planimod.ui.printing.PlanimodPrintFactory;
import com.planimod.ui.printing.PlanimodPrintUtil;

/**
 * This class create a Employee Print Factory. It create a print listing
 * employees.
 * 
 * @author patapouf
 * 
 */
public class EmployeeBySeniorityPrintFactory extends PlanimodPrintFactory {

	/**
	 * Hold employees list.
	 */
	private Collection<EmployeePreference> employees = null;

	/**
	 * Used for localization.
	 */
	private Localized localized = Localized
			.load(EmployeeBySeniorityPrintFactory.class);

	/**
	 * Create a new Factory.
	 * 
	 * @param jobName
	 */
	public EmployeeBySeniorityPrintFactory() {
		super(Localized.get(EmployeeBySeniorityPrintFactory.class,
				"EmployeeBySeniorityPrintFactory.jobName")); //$NON-NLS-1$

		setTitle(this.localized.get("EmployeeBySeniorityPrintFactory.title")); //$NON-NLS-1$
	}

	/**
	 * This implementation create a employees print.
	 */
	@Override
	protected Print createBodyArea() {

		// Retrieved a list of employees
		if (this.employees == null || this.employees.size() == 0) {
			return new TextPrint(""); //$NON-NLS-1$
		}
		List<EmployeePreference> list = new ArrayList<EmployeePreference>(
				this.employees);
		Collections.sort(list, EmployeePreferenceComparators.bySeniority());

		// Create Grid holding the employees list
		GridPrint grid = new GridPrint();

		ColumnPrint column = new ColumnPrint(grid, 2, 15);
		column.setCompressed(false);

		// Set the Grid Look
		DefaultGridLook look = new DefaultGridLook();
		LineBorder border = new LineBorder();
		border.setGapSize(0);
		look.setCellBorder(border);
		look.setCellPadding(3, 3);
		grid.setLook(look);
		grid.setCellClippingEnabled(false);

		// Add column for count
		grid.addColumn("left:preferred"); //$NON-NLS-1$
		grid.addColumn("left:preferred"); //$NON-NLS-1$
		grid.addColumn("left:default:grow"); //$NON-NLS-1$
		grid.addColumn("left:preferred"); //$NON-NLS-1$

		grid.addHeader(new TextPrint(PlanimodPrintUtil.SPACE,
				FontDataUtil.BOLD));
		grid.addHeader(new TextPrint(this.localized
				.get("EmployeeBySeniorityPrintFactory.column.hireDate"), //$NON-NLS-1$
				FontDataUtil.BOLD));
		grid.addHeader(new TextPrint(this.localized
				.get("EmployeeBySeniorityPrintFactory.column.name"), //$NON-NLS-1$
				FontDataUtil.BOLD));
		grid.addHeader(new TextPrint(this.localized
				.get("EmployeeBySeniorityPrintFactory.column.refId"), //$NON-NLS-1$
				FontDataUtil.BOLD));

		// Draw the employees list
		int count = 1;
		for (EmployeePreference e : list) {
			grid.add(new TextPrint(Integer.toString(count), FontDataUtil.DEFAULT));
			grid.add(new TextPrint(PlanimodPrintUtil.getHireDate(e),
					FontDataUtil.DEFAULT));
			grid.add(new TextPrint(PlanimodPrintUtil.getName(e.getEmployee()),
					FontDataUtil.DEFAULT));
			grid.add(new TextPrint(PlanimodPrintUtil.getRefId(e.getEmployee()),
					FontDataUtil.DEFAULT));
			count++;
		}
		return column;
	}

	/**
	 * Returns the employees list.
	 * 
	 * @return employees list or null
	 */
	public Collection<EmployeePreference> getEmployees() {
		return Collections.unmodifiableCollection(this.employees);
	}

	/**
	 * Sets the list of employees to print.
	 * 
	 * @param employees
	 *            a list or null
	 */
	public void setEmployees(Collection<EmployeePreference> employees) {
		this.employees = employees;
	}

}
