/* 
 * Copyright 2015 Adobe.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.adobe.ags.curly.model;

import com.sun.javafx.collections.ObservableListWrapper;
import java.util.ArrayList;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ReadOnlyListProperty;
import javafx.beans.property.ReadOnlyListWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;

public abstract class RunnerResult<T extends RunnerResult> {
    final private DoubleProperty percentSuccess = new SimpleDoubleProperty(0);
    final private DoubleProperty percentComplete = new SimpleDoubleProperty(0);
    final private BooleanProperty started = new SimpleBooleanProperty(false);
    final private ObservableList<ObservableValue> reportRow = new ObservableListWrapper<>(new ArrayList<>());
    final private ObservableList<T> details = new ObservableListWrapper<>(new ArrayList<>());
    
    public DoubleProperty percentSuccess() {
        return percentSuccess;
    };
    public DoubleProperty percentComplete() {
        return percentComplete;
    }
    public BooleanProperty started() {
        return started;
    }
    public BooleanBinding completed() {
        return Bindings.greaterThanOrEqual(percentComplete(), 1.0);
    }
    public BooleanBinding completelySuccessful() {
        return Bindings.greaterThanOrEqual(percentSuccess(), 1.0);
    }
    public ObservableList<ObservableValue> reportRow() {
        return reportRow;
    }
    public ReadOnlyListProperty<T> getDetails() {
        return new ReadOnlyListWrapper<T>(details);
    }
    public void addDetail(T detail) {
        details.add(detail);
        Platform.runLater(()->trackSummaryAgainstAttionalDetail(detail));
    }
    
    private DoubleBinding completionSummary;
    private DoubleBinding successSummary;
    private void trackSummaryAgainstAttionalDetail(T detail) {
        if (details.size() == 1) {
            completionSummary = detail.percentComplete().add(0);
            successSummary = detail.percentSuccess().add(0);
        } else {
            completionSummary = completionSummary.add(detail.percentComplete());
            successSummary = successSummary.add(detail.percentSuccess());
        }
        percentComplete.bind(completionSummary.divide(details.size()));
        percentSuccess.bind(successSummary.divide(details.size()));
    }
    abstract public String toHtml(int level);
}
@@ -1309,16 +1309,17 @@
 sult%3E %7B%0A
+%0A
     fina
@@ -1751,20 +1751,16 @@
 t%3C%3E());%0A
-    
 %0A    pub
@@ -1833,16 +1833,22 @@
 s;%0A    %7D
+%0A%0A    
 ;%0A    pu
@@ -1917,32 +1917,33 @@
 Complete;%0A    %7D%0A
+%0A
     public Boole
@@ -1987,32 +1987,33 @@
  started;%0A    %7D%0A
+%0A
     public Boole
@@ -2102,32 +2102,33 @@
 (), 1.0);%0A    %7D%0A
+%0A
     public Boole
@@ -2227,32 +2227,33 @@
 (), 1.0);%0A    %7D%0A
+%0A
     public Obser
@@ -2317,32 +2317,33 @@
 eportRow;%0A    %7D%0A
+%0A
     public ReadO
@@ -2415,17 +2415,16 @@
 Wrapper%3C
-T
 %3E(detail
@@ -2429,24 +2429,25 @@
 ils);%0A    %7D%0A
+%0A
     public v
@@ -2533,10 +2533,12 @@
 r(()
+ 
 -%3E
+ 
 trac
@@ -2583,20 +2583,16 @@
 ;%0A    %7D%0A
-    
 %0A    pri
@@ -2663,32 +2663,33 @@
 successSummary;%0A
+%0A
     private void
@@ -2751,27 +2751,33 @@
 if (
-details.size() == 1
+completionSummary == null
 ) %7B%0A
@@ -2845,69 +2845,8 @@
 0);%0A
-            successSummary = detail.percentSuccess().add(0);%0A
     
@@ -2935,24 +2935,150 @@
 omplete());%0A
+        %7D%0A        if (successSummary == null) %7B%0A            successSummary = detail.percentSuccess().add(0);%0A        %7D else %7B%0A
             
@@ -3291,24 +3291,25 @@
 ()));%0A    %7D%0A
+%0A
     abstract
@@ -3343,8 +3343,9 @@
 evel);%0A%7D
+%0A

Fix NPE resulting from recent code changes