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

import com.patrikdufresne.jface.databinding.conversion.AcronymConverter;
import com.patrikdufresne.util.DateFormatRegistry;
import com.patrikdufresne.util.Localized;
import com.planimod.core.Employee;
import com.planimod.core.EmployeePreference;
import com.planimod.core.Position;
import com.planimod.core.Section;
import com.planimod.core.Team;
import com.planimod.core.comparators.EmployeePreferenceComparators;
import com.planimod.ui.databinding.Converters;
import com.planimod.ui.views.employees.EmployeeBySectionPrintFactory;

/**
 * Utility class for printing.
 * 
 * @author Patrik Dufresne
 * 
 */
public class PlanimodPrintUtil {

	public static final String ANNOTATION = " *"; //$NON-NLS-1$

	public static final String ANNOTATION_NO_SPACE = "*"; //$NON-NLS-1$

	public static final String EMPTY = ""; //$NON-NLS-1$

	public static final String SPACE = " "; //$NON-NLS-1$

	public static final String X = "X"; //$NON-NLS-1$

	public static String getAcronym(String str) {
		return (String) AcronymConverter.getInstance().convert(str);
	}

	public static String getAcronym(Team t) {
		return getAcronym(getName(t));
	}

	public static String getFirstPreferredTeamAcronym(
			EmployeePreference employee) {
		if (employee.getPreferredTeam() != null
				&& employee.getPreferredTeam().size() > 0) {
			return getAcronym(employee.getPreferredTeam().get(0));
		}
		return EMPTY;
	}

	/**
	 * This function return the employee's comments if available. Otherwise, it
	 * return an empty string.
	 * 
	 * @param employee
	 * @return
	 */
	public static String getHireDate(Employee employee) {
		if (employee.getHireDate() != null) {
			return DateFormatRegistry.format(employee.getHireDate(),
					DateFormatRegistry.DATE | DateFormatRegistry.ISO);
		}
		return EMPTY;
	}

	/**
	 * This function return the hire date of the employee if the employee has
	 * preferences.
	 * 
	 * @param pref
	 *            the employee preferences.
	 * @return the hiredate or empty string.
	 */
	public static String getHireDate(EmployeePreference pref) {
		if (EmployeePreferenceComparators.hasPreferences(pref)) {
			return getHireDate(pref.getEmployee());
		}
		return EMPTY;
	}

	/**
	 * This function return the employee's name if available. Otherwise, it
	 * return an empty string.
	 * 
	 * @param employee
	 * @return
	 */
	public static String getName(Employee employee) {
		String name = "";//$NON-NLS-1$
		if (employee.getFirstname() != null) {
			name += employee.getFirstname();
		}
		if (employee.getLastname() != null) {
			if (employee.getFirstname() != null) {
				name += " "; //$NON-NLS-1$
			}
			name += employee.getLastname();
		}
		return name;
	}

	public static String getName(Employee employee, boolean annotation) {
		return getName(employee) + (annotation ? ANNOTATION : EMPTY);
	}

	/**
	 * This function return the position name.
	 * 
	 * @return a string
	 */
	public static String getName(Position position) {
		if (position != null && position.getName() != null) {
			return position.getName();
		}
		return EMPTY;
	}

	/**
	 * This function is called to return the section name.
	 * 
	 * @param section
	 *            the section or null for "no section"
	 * @return
	 */
	public static String getName(Section section) {
		if (section != null) {
			return (String) Converters.removeFrontNumber().convert(
					section.getName());
		}
		return Localized.get(EmployeeBySectionPrintFactory.class,
				"EmployeeBySectionPrintFactory.noSection"); //$NON-NLS-1$
	}

	/**
	 * Return a string representing the Team.
	 * 
	 * @param team
	 *            the team
	 * @return the team's name or empty string.
	 */
	public static String getName(Team team) {
		if (team.getName() != null) {
			return (String) Converters.removeFrontNumber().convert(
					team.getName());
		}
		return EMPTY;
	}

	/**
	 * Return a string representing the Team.
	 * 
	 * @param team
	 *            the team
	 * @return the team's name or empty string.
	 */
	public static String getName(Team team, int count) {
		if (team.getName() != null) {
			return String.format(
					"%s (%d)", getName(team), Integer.valueOf(count)); //$NON-NLS-1$
		}
		return EMPTY;
	}

	/**
	 * Returns the employee ref id.
	 * 
	 * @param employee
	 * @return
	 */
	public static String getRefId(Employee employee) {
		if (employee.getRefId() != null) {
			return employee.getRefId();
		}
		return ""; //$NON-NLS-1$
	}

	public static String getName(Employee e, Team team) {
		if (team != null) {
			return String.format("%s (%s)", getName(e), getName(team)); //$NON-NLS-1$
		}
		return getName(e);
	}

}
