package org.talend.dataprep.transformation.api.action.metadata;

import java.io.IOException;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.talend.dataprep.api.dataset.DataSetRow;
import org.talend.dataprep.api.type.Type;

@Component(Negate.ACTION_BEAN_PREFIX + Negate.NEGATE_ACTION_NAME)
public class Negate implements ActionMetadata {

    public static final String COLUMN_NAME_PARAMETER = "column_name"; //$NON-NLS-1$

    public static final String NEGATE_ACTION_NAME = "negate"; //$NON-NLS-1$

    public static final ActionMetadata INSTANCE = new Negate();

    // Please do not instantiate this class, it is spring Bean automatically instantiated.
    public Negate() {
    }

    @Override
    public String getName() {
        return NEGATE_ACTION_NAME;
    }

    @Override
    public String getCategory() {
        return "boolean"; //$NON-NLS-1$
    }

    @Override
    public Item[] getItems() {
        return new Item[0];
    }

    @Override
    public Parameter[] getParameters() {
        return new Parameter[] { new Parameter(COLUMN_NAME_PARAMETER, Type.STRING.getName(), StringUtils.EMPTY) };
    }

    @Override
    public Consumer<DataSetRow> create(Map<String, String> parsedParameters) {
        return row -> {
            String columnName = parsedParameters.get(COLUMN_NAME_PARAMETER);
            String value = row.get(columnName);

            if (value != null && (value.trim().equalsIgnoreCase("true") || value.trim().equalsIgnoreCase("false"))) { //$NON-NLS-1$//$NON-NLS-2$
                Boolean boolValue = Boolean.valueOf(value);
                row.set(columnName, toProperCase("" + !boolValue)); //$NON-NLS-1$
            }
        };
    }

    // TODO move this
    protected static String toProperCase(String from) {
        java.io.StringReader in = new java.io.StringReader(from.toLowerCase());
        boolean precededBySpace = true;
        StringBuilder properCase = new StringBuilder();
        while (true) {
            try {
                int i = in.read();
                if (i == -1) {
                    break;
                }
                char c = (char) i;
                if (c == ' ' || c == '"' || c == '(' || c == '.' || c == '/' || c == '\\' || c == ',') {
                    properCase.append(c);
                    precededBySpace = true;
                } else {
                    if (precededBySpace) {
                        properCase.append(Character.toUpperCase(c));
                    } else {
                        properCase.append(c);
                    }
                    precededBySpace = false;
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        return properCase.toString();
    }

    @Override
    public Set<Type> getCompatibleColumnTypes() {
        return Collections.singleton(Type.BOOLEAN);
    }

}

@@ -86,16 +86,45 @@
 eption;%0A
+import java.io.StringReader;%0A
 import j
@@ -270,16 +270,101 @@
 gUtils;%0A
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A
 import o
@@ -637,22 +637,127 @@
 c final 
-String
+Log            LOGGER                = LogFactory.getLog(Negate.class);%0A%0A    public static final String        
  COLUMN_
@@ -787,16 +787,33 @@
 n_name%22;
+                 
  //$NON-
@@ -851,16 +851,24 @@
  String 
+        
 NEGATE_A
@@ -878,16 +878,19 @@
 ON_NAME 
+   
 = %22negat
@@ -892,16 +892,38 @@
 negate%22;
+                      
  //$NON-
@@ -978,16 +978,29 @@
 NSTANCE 
+             
 = new Ne
@@ -2214,24 +2214,16 @@
         
-java.io.
 StringRe
@@ -2236,24 +2236,16 @@
 n = new 
-java.io.
 StringRe
@@ -3102,74 +3102,109 @@
  // 
-TODO Auto-generated catch block%0A                e.printStackTrace(
+should not occurs when reading a String%0A                LOGGER.warn(%22This error should not occurs%22, e
 );%0A 

use logger to fix sonarqube anomaly