/**
 * 
 */
package com.planimod.ui.views.admin.product;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.window.IShellProvider;

import com.patrikdufresne.managers.ManagedObject;
import com.patrikdufresne.managers.ManagerException;
import com.patrikdufresne.util.Localized;
import com.planimod.core.IntegrityException;
import com.planimod.core.PlanimodManagers;
import com.planimod.core.Position;
import com.planimod.core.Product;
import com.planimod.core.ProductPosition;
import com.planimod.ui.PlanimodPolicy;
import com.planimod.ui.theme.Resources;
import com.planimod.ui.views.AbstractAddAction;

/**
 * Action to create a new product-position object and add it to the database.
 * 
 * @author Patrik Dufresne
 * 
 */
public class AddProductPositionAction extends AbstractAddAction {

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

	/**
	 * The product to associated with.
	 */
	private Product product;

	/**
	 * Create a new action.
	 * 
	 * @param shellProvider
	 *            a shell provider in case the action need to display a message
	 *            box.
	 */
	public AddProductPositionAction(PlanimodManagers managers,
			IShellProvider shellProvider, ISelectionProvider selectionProvider) {
		super(managers, shellProvider, selectionProvider);
		setText(Localized.get(AddProductPositionAction.class,
				"AddProductPositionAction.text")); //$NON-NLS-1$
		setToolTipText(Localized.get(AddProductPositionAction.class,
				"AddProductPositionAction.tooltipText")); //$NON-NLS-1$
		setImageDescriptor(Resources
				.getImageDescriptor(Resources.ICON_LIST_ADD_16));
	}

	/**
	 * This implementation always return true.
	 * 
	 * @see net.ekwos.gymkhana.ui.views.AbstractAddAction#canCreateObject()
	 */
	@Override
	protected boolean canCreateObject() {
		return this.product != null;
	}

	/**
	 * This implementation create rider object.
	 * 
	 * @throws IntegrityException
	 *             if the objects can be created for integrity reason.
	 * 
	 * @see net.ekwos.gymkhana.ui.views.AbstractAddAction#createObjects()
	 */
	@Override
	protected List<? extends ManagedObject> createObjects()
			throws IntegrityException {
		// Create product position object
		ProductPosition pp = new ProductPosition();
		pp.setProduct(this.product);
		pp.setNumber(Integer.valueOf(1));

		// Find a proper position object
		List<Position> list;
		try {
			list = getManagers().getPositionManager().list();
		} catch (ManagerException e) {
			PlanimodPolicy.showException(e);
			return new ArrayList<ManagedObject>(0);
		}

		// Raise exception is not position is found.
		if (list.size() == 0) {
			throw new IntegrityException(Localized.get(
					AddProductPositionAction.class,
					"AddProductPositionAction.noPositionFound")); //$NON-NLS-1$
		}

		pp.setPosition(list.get(0));
		return Arrays.asList(pp);
	}

	/**
	 * Returns the product to to create the product-position object.
	 * 
	 * @return
	 */
	public Product getProduct() {
		return this.product;
	}

	/**
	 * Sets the product used to create the product-position object.
	 * 
	 * @param product
	 */
	public void setProduct(Product product) {
		firePropertyChange(PRODUCT, this.product, this.product = product);
		setEnabled(canCreateObject());
	}
}