class ExpressionFunctionUtils {

    static BlockStmt getFieldRefExpressionFromDefineFunctionBlockStmt(final String variableName,
                                                                      final FieldRef fieldRef,
                                                                      final ClassOrInterfaceType returnedType,
                                                                      final LinkedHashMap<String,
                                                                              ClassOrInterfaceType> parameterNameTypeMap) {
        final BlockStmt toReturn = new BlockStmt();
        String fieldNameToRef = fieldRef.getField().getValue();

        // field_ref
        NameExpr nameExpression = new NameExpr(fieldNameToRef);

        // condition
        BinaryExpr condition = new BinaryExpr();
        condition.setLeft(nameExpression);
        condition.setRight(new NullLiteralExpr());
        condition.setOperator(BinaryExpr.Operator.NOT_EQUALS);
        // then
        CastExpr thenExpr = new CastExpr();
        thenExpr.setType(returnedType);
        // ((returnedType)) org.kie.pmml.commons.utils.ConverterTypeUtil.convert((returnedType).class, (variable_name))
        MethodCallExpr converterCallExpression = new MethodCallExpr();
        converterCallExpression.setName("convert");
        converterCallExpression.setScope(CONVERTER_TYPE_UTIL_FIELD_ACCESSOR_EXPR);
        ClassExpr expectedClassExpression = new ClassExpr();
        expectedClassExpression.setType(returnedType);
        converterCallExpression.setArguments(NodeList.nodeList(expectedClassExpression, nameExpression));
        thenExpr.setExpression(converterCallExpression);
        // else
        com.github.javaparser.ast.expr.Expression elseExpression = fieldRef.getMapMissingTo() != null ?
                new StringLiteralExpr(fieldRef.getMapMissingTo()) : new NullLiteralExpr();
        CastExpr elseExpr = new CastExpr();
        elseExpr.setType(returnedType);
        elseExpr.setExpression(elseExpression);

        ConditionalExpr initializer = new ConditionalExpr();
        initializer.setCondition(condition);
        initializer.setThenExpr(thenExpr);
        initializer.setElseExpr(elseExpr);

        // (returnedType) (variableName) = (returnedType) field_ref;
        VariableDeclarator variableDeclarator = new VariableDeclarator();
        variableDeclarator.setType(returnedType);
        variableDeclarator.setName(variableName);
        variableDeclarator.setInitializer(initializer);

        VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr();
        variableDeclarationExpr.setVariables(NodeList.nodeList(variableDeclarator));
        toReturn.addStatement(variableDeclarationExpr);

        return toReturn;
    }

    private  ExpressionFunctionUtils();

}