package sophena.rcp.editors.results.compare;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;

import sophena.calc.Comparison;
import sophena.calc.CostResult;
import sophena.calc.CostResultItem;
import sophena.model.ProductArea;
import sophena.rcp.Labels;
import sophena.rcp.utils.UI;
import sophena.utils.Num;

class CostDetailsTable {

	private final Comparison result;

	private CostDetailsTable(Comparison result) {
		this.result = result;
	}

	static CostDetailsTable of(Comparison result) {
		return new CostDetailsTable(result);
	}

	void render(Composite body, FormToolkit tk) {
		Composite comp = UI.formSection(body, tk,
				"Investitionskosten");
		Table table = new Table(result);
		createItems(table);
		table.render(comp);
	}

	private void createItems(Table table) {
		Map<ProductArea, double[]> details = calculate();
		double[] totals = new double[result.projects.length];
		for (ProductArea area : ProductArea.values()) {
			double[] values = details.get(area);
			if (values == null)
				continue;
			for (int i = 0; i < totals.length; i++) {
				totals[i] += values[i];
			}
			table.row(Labels.get(area),
					i -> Num.intStr(values[i]) + " EUR");
		}
		table.emptyRow();
		table.boldRow("Investitionssumme",
				i -> Num.intStr(totals[i]) + " EUR");
	}

	private Map<ProductArea, double[]> calculate() {
		int length = result.projects.length;
		Map<ProductArea, double[]> r = new HashMap<>();
		for (ProductArea pa : ProductArea.values()) {
			r.put(pa, new double[length]);
		}
		for (int i = 0; i < length; i++) {
			CostResult cr = result.results[i].costResult;
			for (CostResultItem item : cr.items) {
				if (item.productType == null
						|| item.costs == null
						|| item.costs.investment == 0d)
					continue;
				double[] values = r.get(item.productType.productArea);
				values[i] += item.costs.investment;
			}
		}
		return r;
	}

}

@@ -43,57 +43,8 @@
 e;%0A%0A
-import java.util.HashMap;%0Aimport java.util.Map;%0A%0A
 impo
@@ -215,30 +215,33 @@
 na.calc.
-Cost
+ProductArea
 Result
-Item
 ;%0Aimport
@@ -823,20 +823,16 @@
 le) %7B%0A%09%09
-Map%3C
 ProductA
@@ -834,27 +834,24 @@
 ductArea
-, double
+Result
 %5B%5D
-%3E
  details
@@ -853,25 +853,23 @@
 tails = 
-calculate
+results
 ();%0A%09%09do
@@ -977,66 +977,34 @@
 %0A%09%09%09
-double%5B%5D values = details.get(area);%0A%09%09%09if (values == null
+if (allZero(details, area)
 )%0A%09%09
@@ -1077,25 +1077,48 @@
 s%5Bi%5D += 
-values%5Bi%5D
+details%5Bi%5D.investmentCosts(area)
 ;%0A%09%09%09%7D%0A%09
@@ -1150,18 +1150,77 @@
 ea),
-%0A%09%09%09%09%09i -%3E
+ i -%3E %7B%0A%09%09%09%09double val = details%5Bi%5D.investmentCosts(area);%0A%09%09%09%09return
  Num
@@ -1230,22 +1230,16 @@
 tStr(val
-ues%5Bi%5D
 ) + %22 EU
@@ -1236,24 +1236,30 @@
 al) + %22 EUR%22
+;%0A%09%09%09%7D
 );%0A%09%09%7D%0A%09%09tab
@@ -1365,20 +1365,16 @@
 private 
-Map%3C
 ProductA
@@ -1380,29 +1380,24 @@
 Area
-, double%5B%5D%3E calculate
+Result%5B%5D results
 () %7B
@@ -1438,20 +1438,16 @@
 ngth;%0A%09%09
-Map%3C
 ProductA
@@ -1453,19 +1453,16 @@
 Area
-, double
+Result
 %5B%5D
-%3E
  r =
@@ -1470,100 +1470,33 @@
 new 
-HashMap%3C%3E();%0A%09%09for (ProductArea pa : ProductArea.values()) %7B%0A%09%09%09r.put(pa, new double
+ProductAreaResult
 %5Blength%5D
 );%0A%09
@@ -1491,22 +1491,17 @@
 %5Blength%5D
-);%0A%09%09%7D
+;
 %0A%09%09for (
@@ -1586,264 +1586,228 @@
 %0A%09%09%09
-for (CostResultItem item : cr.items) %7B%0A%09%09%09%09if (item.productType == null%0A%09%09%09%09%09%09%7C%7C item.costs == null%0A%09%09%09%09%09%09%7C%7C item.costs.investment == 0d)%0A%09%09%09%09%09continue;%0A%09%09%09%09double%5B%5D values = r.get(item.productType.productArea);%0A%09%09%09%09values%5Bi%5D += item.costs.investment;%0A%09%09%09%7D
+r%5Bi%5D = ProductAreaResult.calculate(cr);%0A%09%09%7D%0A%09%09return r;%0A%09%7D%0A%0A%09private boolean allZero(ProductAreaResult%5B%5D r, ProductArea area) %7B%0A%09%09for (ProductAreaResult par : r) %7B%0A%09%09%09if (par.investmentCosts(area) != 0)%0A%09%09%09%09return false;
 %0A%09%09%7D
@@ -1812,21 +1812,24 @@
 %09%7D%0A%09%09return 
-r
+true
 ;%0A%09%7D%0A%0A%7D%0A

use ProductAreaResult in project comparisons