/*******************************************************************************
 * Copyright (c) 2014 MEDEVIT.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     MEDEVIT <office@medevit.at> - initial API and implementation
 ******************************************************************************/
package at.medevit.ch.artikelstamm.elexis.common.ui.provider;

import java.util.List;

import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.wb.swt.ResourceManager;

import at.medevit.ch.artikelstamm.ui.ArtikelstammLabelProvider;
import ch.artikelstamm.elexis.common.ArtikelstammItem;
import ch.elexis.core.data.activator.CoreHub;
import ch.elexis.core.model.IOrderEntry;
import ch.elexis.core.model.IStockEntry;
import ch.elexis.core.services.IStockService.Availability;
import ch.elexis.core.ui.UiDesk;

/**
 * {@link LabelProvider} that extends the basic {@link ArtikelstammLabelProvider} to consider the
 * stock status of articles. Applicable to Elexis v2.1 only.
 */
public class LagerhaltungArtikelstammLabelProvider extends ArtikelstammLabelProvider
		implements IColorProvider {
	
	private Image blackBoxedImage = ResourceManager.getPluginImage("at.medevit.ch.artikelstamm.ui",
		"/rsc/icons/flag-black.png");
	
	@Override
	public Image getImage(Object element){
		ArtikelstammItem ai = (ArtikelstammItem) element;
		if (ai.isBlackBoxed())
			return blackBoxedImage;
		return super.getImage(element);
	}
	
	@Override
	public String getText(Object element){
		ArtikelstammItem ai = (ArtikelstammItem) element;
		Integer availability = CoreHub.getStockService().getCumulatedStockForArticle(ai);
		if (availability != null) {
			return ai.getLabel() + " (LB: " + availability + ")";
		}
		return ai.getLabel();
		
	}
	
	/**
	 * Lagerartikel are shown in blue, articles that should be ordered are shown in red
	 */
	@Override
	public Color getForeground(Object element){
		ArtikelstammItem ai = (ArtikelstammItem) element;
		
		List<? extends IStockEntry> stockEntries =
			CoreHub.getStockService().findAllStockEntriesForArticle(ai.storeToString());
		if (!stockEntries.isEmpty()) {
			for (IStockEntry iStockEntry : stockEntries) {
				IOrderEntry order =
					CoreHub.getOrderService().findOpenOrderEntryForStockEntry(iStockEntry);
				if (order != null) {
					return UiDesk.getColor(UiDesk.COL_LIGHTBLUE);
				}
			}
		}
		
		Availability availability =
			CoreHub.getStockService().getCumulatedAvailabilityForArticle(ai);
		if (availability != null) {
			switch (availability) {
			case CRITICAL_STOCK:
			case OUT_OF_STOCK:
				return UiDesk.getColor(UiDesk.COL_RED);
			default:
				return UiDesk.getColor(UiDesk.COL_BLUE);
			}
		}
		return null;
	}
	
	@Override
	public Color getBackground(Object element){
		ArtikelstammItem ai = (ArtikelstammItem) element;
		if (ai.isBlackBoxed())
			return UiDesk.getColor(UiDesk.COL_GREY60);
		return super.getBackground(element);
	}
	
}

@@ -588,32 +588,8 @@
 r;%0A%0A
-import java.util.List;%0A%0A
 impo
@@ -972,90 +972,8 @@
 ub;%0A
-import ch.elexis.core.model.IOrderEntry;%0Aimport ch.elexis.core.model.IStockEntry;%0A
 impo
@@ -2190,411 +2190,8 @@
 %0A%09%09%0A
-%09%09List%3C? extends IStockEntry%3E stockEntries =%0A%09%09%09CoreHub.getStockService().findAllStockEntriesForArticle(ai.storeToString());%0A%09%09if (!stockEntries.isEmpty()) %7B%0A%09%09%09for (IStockEntry iStockEntry : stockEntries) %7B%0A%09%09%09%09IOrderEntry order =%0A%09%09%09%09%09CoreHub.getOrderService().findOpenOrderEntryForStockEntry(iStockEntry);%0A%09%09%09%09if (order != null) %7B%0A%09%09%09%09%09return UiDesk.getColor(UiDesk.COL_LIGHTBLUE);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09%0A
 %09%09Av

Make order of already ordered items configurable