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

import java.beans.PropertyDescriptor;

import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;

import com.planimod.core.EmployeePreference;
import com.planimod.core.Product;
import com.planimod.core.ProductionEvent;

public class ProductLabelValueProperty extends BeanComputedValueProperty {

	public ProductLabelValueProperty() {
		super(properties(), String.class);
	}

	private static final Object EMPTY = ""; //$NON-NLS-1$
	private static final String DASH = " - "; //$NON-NLS-1$

	/**
	 * Static function used to return the list of property descriptors for this
	 * computed value property.
	 * 
	 * @return
	 */
	private static PropertyDescriptor[] properties() {
		return new PropertyDescriptor[] {
				BeanPropertyHelper.getPropertyDescriptor(
						EmployeePreference.class,
						EmployeePreference.PREFERRED_POSITION),
				BeanPropertyHelper.getPropertyDescriptor(
						EmployeePreference.class,
						EmployeePreference.PREFERRED_POSITION_TEAM) };
	}

	@Override
	protected Object doGetValue(Object source) {
		Product product = (Product) source;

		StringBuilder buf = new StringBuilder();
		String family = product.getFamily() == null
				|| product.getFamily().isEmpty() ? null : product.getFamily();
		String name = product.getName() == null || product.getName().isEmpty() ? null
				: product.getName();
		String refId = product.getRefId() == null
				|| product.getRefId().isEmpty() ? null : product.getRefId();
		if (family != null) {
			buf.append(family);
		}
		if (name != null) {
			if (family != null) {
				buf.append(DASH);
			}
			buf.append(name);
		}
		if (refId != null) {
			if (family != null || name != null) {
				buf.append(DASH);
			}
			buf.append(refId);
		}

		return buf.toString();
	}

}
