package org.talend.dataprep.transformation.api.transformer.suggestion.rules;

import static org.talend.dataprep.transformation.api.transformer.suggestion.SuggestionEngineRule.NEGATIVE;
import static org.talend.dataprep.transformation.api.transformer.suggestion.SuggestionEngineRule.POSITIVE;
import static org.talend.dataprep.transformation.api.transformer.suggestion.rules.GenericRule.GenericRuleBuilder.forActions;

import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.talend.dataprep.api.dataset.statistics.PatternFrequency;
import org.talend.dataprep.transformation.api.action.metadata.math.*;
import org.talend.dataprep.transformation.api.transformer.suggestion.SuggestionEngineRule;

@Component
public class IntegerRules extends BasicRules {

    /**
     * @return A {@link SuggestionEngineRule rule} that hides "absolute" actions if all numbers >=0.
     */
    @Bean
    public static SuggestionEngineRule absoluteRule() {
        return forActions(Absolute.ABSOLUTE_ACTION_NAME) //
                .when(IS_NUMERIC) //
                .then(columnMetadata -> {
                    if (columnMetadata.getStatistics().getMin() >= 0) {
                        return NEGATIVE;
                    } else {
                        return POSITIVE;
                    }
                }) //
                .build();
    }

    /**
     * @return A {@link SuggestionEngineRule rule} that hides floor, ceil & round actions if all integers.
     */
    @Bean
    public static SuggestionEngineRule integerRule() {
        return forActions(RoundDown.ACTION_NAME, RoundHalfUp.ACTION_NAME) //
                .when(IS_NUMERIC) //
                .then(columnMetadata -> {
                    final List<PatternFrequency> patterns = columnMetadata.getStatistics().getPatternFrequencies();
                    if (patterns.size() == 1 && patterns.get(0).getPattern().indexOf('.') > 0) {
                        return POSITIVE;
                    } else {
                        return NEGATIVE;
                    }
                }) //
                .build();
    }
}

@@ -671,17 +671,182 @@
 ta.math.
-*
+Absolute;%0Aimport org.talend.dataprep.transformation.api.action.metadata.math.RoundDown;%0Aimport org.talend.dataprep.transformation.api.action.metadata.math.RoundHalfUp
 ;%0Aimport
@@ -2056,36 +2056,80 @@
     
-if (patterns.size() == 1 && 
+for (PatternFrequency pattern : patterns) %7B%0A                        if (
 patt
@@ -2189,32 +2189,36 @@
                 
+    
 return POSITIVE;
@@ -2234,32 +2234,29 @@
             
-%7D else %7B
+    %7D
 %0A           
@@ -2264,36 +2264,17 @@
         
-    return NEGATIVE;
+%7D
 %0A       
@@ -2278,33 +2278,48 @@
                 
-%7D
+return NEGATIVE;
 %0A               

fix bug regarding rule suggestion : with more than one pattern on numeric column, no actions were suggested