package sophena.math.energetic;

import sophena.Defaults;
import sophena.calc.EnergyResult;
import sophena.calc.ProjectResult;
import sophena.model.Fuel;
import sophena.model.Producer;

public class PrimaryEnergyFactor {

	private PrimaryEnergyFactor() {
	}

	public static double get(ProjectResult result) {
		if (result == null || result.project == null
				|| result.energyResult == null)
			return 0;
		double usedHeat = UsedHeat.get(result);
		if (usedHeat == 0)
			return 0;
		double sum = 0;
		EnergyResult r = result.energyResult;
		for (Producer p : r.producers) {
			double producedHeat = r.totalHeat(p);
			double fuelEnergy = result.fuelUsage.getInKWh(p);
			double fuelFactor = getFuelFactor(p);
			double usedElectricity = UsedElectricity.get(producedHeat,
					result.project.costSettings);
			double generatedElectricity = GeneratedElectricity.get(p, result);
			sum += ((fuelEnergy * fuelFactor)
					+ (usedElectricity - generatedElectricity)
							* Defaults.PRIMARY_ENERGY_FACTOR_ELECTRICITY);
		}
		double pf = sum / usedHeat;
		return pf < 0 ? 0 : pf;
	}

	private static double getFuelFactor(Producer p) {
		if (p == null || p.fuelSpec == null)
			return 0d;
		Fuel fuel = p.fuelSpec.fuel;
		return fuel != null ? fuel.primaryEnergyFactor : 0d;
	}
}

@@ -55,42 +55,8 @@
 ts;%0A
-import sophena.calc.EnergyResult;%0A
 impo
@@ -259,21 +259,16 @@
 Result r
-esult
 ) %7B%0A%09%09if
@@ -270,21 +270,16 @@
 %0A%09%09if (r
-esult
  == null
@@ -283,21 +283,16 @@
 ull %7C%7C r
-esult
 .project
@@ -308,21 +308,54 @@
 %09%09%09%09%7C%7C r
-esult
+.project.costSettings == null%0A%09%09%09%09%7C%7C r
 .energyR
@@ -416,21 +416,16 @@
 at.get(r
-esult
 );%0A%09%09if 
@@ -477,48 +477,8 @@
 ;%0A%09%09
-EnergyResult r = result.energyResult;%0A%09%09
 for 
@@ -492,16 +492,29 @@
 er p : r
+.energyResult
 .produce
@@ -523,48 +523,32 @@
 ) %7B%0A
+%0A
 %09%09%09
-double producedHeat = r.totalHeat(p);
+// for consumed fuel
 %0A%09%09%09
@@ -568,21 +568,16 @@
 ergy = r
-esult
 .fuelUsa
@@ -599,119 +599,252 @@
 %0A%09%09%09
-double fuelFactor = getFuelFactor(p);%0A%09%09%09double usedElectricity = UsedElectricity.get(producedHeat,
+sum += (fuelEnergy * factor(p, 0));%0A%0A%09%09%09// for used electricity%0A%09%09%09double genHeat = r.energyResult.totalHeat(p);%0A%09%09%09double usedElectricity = UsedElectricity.get(%0A%09%09%09%09%09genHeat, r.project.costSettings);%0A%09%09%09sum += (usedElectricity * factor(
 %0A%09%09%09%09%09r
-esult
 .pro
@@ -860,28 +860,95 @@
 Settings
+.usedElectricity,%0A%09%09%09%09%09Defaults.PRIMARY_ENERGY_FACTOR_ELECTRICITY)
 );%0A
+%0A
 %09%09%09
-double
+// for
  generat
@@ -941,32 +941,58 @@
 // for generated
+ electricity%0A%09%09%09double gen
 Electricity = Ge
@@ -1022,86 +1022,91 @@
 p, r
-esult
 );%0A%09%09%09
-sum += ((fuelEnergy * fuelFactor)%0A%09%09%09%09%09+ (usedElectricity - generat
+if (genElectricity %3E 0) %7B%0A%09%09%09%09sum -= (genElectricity * factor(%0A%09%09%09%09%09%09p.produc
 edEl
@@ -1118,19 +1118,16 @@
 city
-)
+,
 %0A%09%09%09%09%09%09
-%09* 
 Defa
@@ -1169,14 +1169,20 @@
 ITY)
+)
 ;%0A
+%09
 %09%09%7D%0A
+%09%09%7D%0A
 %09%09do
@@ -1264,16 +1264,9 @@
 ble 
-getFuelF
+f
 acto
@@ -1277,16 +1277,35 @@
 oducer p
+, double defaultVal
 ) %7B%0A%09%09if
@@ -1353,71 +1353,178 @@
 urn 
-0d;%0A%09%09Fuel fuel = p.fuelSpec.fuel;%0A%09%09return fuel != null ? fuel
+defaultVal;%0A%09%09return factor(p.fuelSpec.fuel, defaultVal);%0A%09%7D%0A%0A%09private static double factor(Fuel f, double defaultVal) %7B%0A%09%09if (f == null)%0A%09%09%09return defaultVal;%0A%09%09return f
 .pri
@@ -1543,13 +1543,8 @@
 ctor
- : 0d
 ;%0A%09%7D

use new electricity settings when calculating the primary energy factor