package com.github.jhpoelen.fbob;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.logging.Level;
import java.util.logging.Logger;

class ValueFactoryCalculated implements ValueFactory {
    private static final Logger LOG = Logger.getLogger(ValueFactoryCalculated.class.getName());

    private final ValueFactory valueFactory;

    public ValueFactoryCalculated(ValueFactory valueFactory) {
        this.valueFactory = valueFactory;
    }

    @Override
    public String groupValueFor(String name, Group group) {
        String value = null;
        String calculatedPropertyName = "predation.efficiency.critical.sp";
        if (StringUtils.equals(name, calculatedPropertyName)) {
            try {
                String ingestionRateMax = "predation.ingestion.rate.max.sp";
                String ingestionRate = valueFactory.groupValueFor(ingestionRateMax, group);
                String maintQB = valueFactory.groupValueFor("popqb.MaintQB", group);
                if (StringUtils.isNotBlank(ingestionRate) && StringUtils.isNotBlank(maintQB)) {
                    float ingestionRateParsed = Float.parseFloat(ingestionRate);
                    if (ingestionRateParsed == 0) {
                        LOG.warning(getMsg(calculatedPropertyName) + ": tried to divide by zero value for [" + ingestionRateMax + "]");
                    } else {
                        value = String.format("%.2f", Float.parseFloat(maintQB) / ingestionRateParsed);
                    }
                }
            } catch (NumberFormatException ex) {
                LOG.log(Level.WARNING, getMsg(calculatedPropertyName), ex);
            }
        } else if (StringUtils.equals(name, "species.relativefecundity.sp")) {
            String relFecundityMean = valueFactory.groupValueFor("fecundity.RelFecundityMean", group);
            String spawningCycles = valueFactory.groupValueFor("fecundity.SpawningCycles", group);
            if (NumberUtils.isParsable(relFecundityMean) && NumberUtils.isParsable(spawningCycles)) {
                value = String.format("%.2f", Float.parseFloat(spawningCycles) * Float.parseFloat(relFecundityMean));
            }
        }
        return value;
    }

    private String getMsg(String name) {
        return "failed to calculate [" + name + "]";
    }

}

@@ -124,16 +124,64 @@
 Utils;%0A%0A
+import java.util.HashMap;%0Aimport java.util.Map;%0A
 import j
@@ -434,16 +434,72 @@
 Factory;
+%0A    private final Map%3CString, ValueFactory%3E factoryMap;
 %0A%0A    pu
@@ -605,82 +605,137 @@
     
-%7D%0A%0A
     
-@Override%0A    public String groupValueFor(String name, Group
+this.factoryMap = new HashMap%3CString, ValueFactory%3E() %7B%7B%0A            put(%22predation.efficiency.critical.sp%22, (name,
  group)
+ -%3E
  %7B%0A 
@@ -745,29 +745,8 @@
     
-String value = null;%0A
     
@@ -829,64 +829,40 @@
     
-if (StringUtils.equals(name, calculatedPropertyName)) %7B%0A
+        String value = null;%0A   
     
@@ -873,14 +873,19 @@
     
+ 
 try %7B%0A
+    
     
@@ -965,32 +965,36 @@
                 
+    
 String ingestion
@@ -1065,24 +1065,28 @@
             
+ 
+   
 String maint
@@ -1134,32 +1134,36 @@
 intQB%22, group);%0A
+    
                 
@@ -1262,16 +1262,20 @@
         
+   
+ 
 float in
@@ -1343,24 +1343,28 @@
             
+    
 if (ingestio
@@ -1379,24 +1379,28 @@
 sed == 0) %7B%0A
+    
             
@@ -1539,24 +1539,28 @@
             
+ 
+   
 %7D else %7B%0A   
@@ -1556,16 +1556,20 @@
  else %7B%0A
+    
         
@@ -1676,34 +1676,42 @@
                 
+   
+ 
 %7D%0A
+    
                 
@@ -1704,32 +1704,36 @@
               %7D%0A
+    
             %7D ca
@@ -1777,24 +1777,28 @@
             
+ 
+   
 LOG.log(Leve
@@ -1849,32 +1849,36 @@
 x);%0A            
+    
 %7D%0A        %7D else
@@ -1875,79 +1875,154 @@
     
-%7D else if (StringUtils.equals(name, %22species.relativefecundity.sp%22)) %7B%0A
+        return value;%0A            %7D);%0A            put(%22species.relativefecundity.sp%22, (name, group) -%3E %7B%0A                String value = null;%0A    
     
@@ -2128,24 +2128,28 @@
             
+   
+ 
 String spawn
@@ -2215,32 +2215,36 @@
 ycles%22, group);%0A
+    
             if (
@@ -2337,32 +2337,36 @@
                 
+    
 value = String.f
@@ -2455,32 +2455,36 @@
 ));%0A            
+ 
+   
 %7D%0A        %7D%0A    
@@ -2469,34 +2469,32 @@
       %7D%0A        
-%7D%0A
         return v
@@ -2493,24 +2493,259 @@
 return value
+;%0A            %7D);%0A        %7D%7D;%0A    %7D%0A%0A    @Override%0A    public String groupValueFor(String name, Group group) %7B%0A        return factoryMap.containsKey(name)%0A            ? factoryMap.get(name).groupValueFor(name, group)%0A            : null
 ;%0A    %7D%0A%0A   

refactor calcalatedvalues