/*
 * Copyright (C) by Courtanet, All Rights Reserved.
 */
package io.doov.core.dsl.meta.ast;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import io.doov.core.dsl.lang.StepWhen;
import io.doov.core.dsl.meta.*;

public class AstLinePercentVisitor extends AstLineVisitor {
    private static final NumberFormat formatter = new DecimalFormat("###.#");

    public AstLinePercentVisitor(StringBuilder stringBuilder, ResourceProvider bundle, Locale locale) {
        super(stringBuilder, bundle, locale);
    }

    @Override
    public void startMetadata(StepWhen metadata, int depth) {
        sb.append(percentage((PredicateMetadata) metadata.stepCondition().getMetadata())+ " ");
        super.startMetadata(metadata, depth);
    }

    @Override
    public void startMetadata(NaryMetadata metadata, int depth) {
        sb.append(formatCurrentIndent());
        sb.append(bundle.get(metadata.getOperator(), locale));
        sb.append(formatNewLine());
        sb.append(percentage(metadata) +" ");
        sb.append("[");
    }

    @Override
    protected String formatLeafMetadata(LeafMetadata metadata) {
        return super.formatLeafMetadata(metadata) +" "+ percentage(metadata);
    }

    private String percentage(PredicateMetadata metadata) {
        int t = metadata.trueEvalCount();
        int f = metadata.falseEvalCount();

        if (f == 0 && t == 0) {
            return "[n/a]";
        }
        else{
            return "[" + formatter.format((t / ((double) t + f))*100) + "]";
        }
    }
}

@@ -386,16 +386,45 @@
 ###.#%22);
+%0A    private boolean endNary;
 %0A%0A    pu
@@ -791,32 +791,302 @@
  depth);%0A    %7D%0A%0A
+    @Override%0A    public void startMetadata(BinaryMetadata metadata, int depth) %7B%0A        super.startMetadata(metadata, depth);%0A        if (metadata.children().get(0).type() == MetadataType.NARY_PREDICATE) %7B%0A            sb.append(percentage(metadata));%0A        %7D%0A    %7D%0A%0A
     @Override%0A  
@@ -1286,24 +1286,194 @@
 NewLine());%0A
+        System.out.println(metadata.getOperator());%0A        if (metadata.getOperator() != DefaultOperator.count && metadata.getOperator() != DefaultOperator.count) %7B%0A    
         sb.a
@@ -1502,15 +1502,20 @@
 ata)
- +%22 %22);
+);%0A        %7D
 %0A   
@@ -1574,125 +1574,402 @@
 ted 
-String formatLeafMetadata(LeafMetadata metadata) %7B%0A        return super.formatLeafMetadata(metadata) +%22 %22+ percentage
+void endMetadata(NaryMetadata metadata, int depth) %7B%0A        super.endMetadata(metadata, depth);%0A        endNary = true;%0A    %7D%0A%0A    @Override%0A    protected String formatLeafMetadata(LeafMetadata metadata) %7B%0A        if (stackPeek() == MetadataType.BINARY_PREDICATE) %7B%0A            return super.formatLeafMetadata(metadata);%0A        %7D%0A        return percentage(metadata) + super.formatLeafMetadata
 (met

fix count sum percentages visualisation