/**
 * File: FunctionExpression.java
 *
 * Author: David Hay (dhay@localmatters.com)
 * Creation Date: Apr 22, 2010
 * Creation Time: 4:27:41 PM
 *
 * Copyright 2010 Local Matters, Inc.
 * All Rights Reserved
 *
 * Last checkin:
 *  $Author$
 *  $Revision$
 *  $Date$
 */
package org.lesscss4j.model.expression;

import java.util.ArrayList;
import java.util.List;

// todo: extend ListExpression
public class FunctionExpression implements Expression {
    private String _name;
    private List<Expression> _arguments;

    public FunctionExpression() {
        this(null);
    }

    public FunctionExpression(String name) {
        _name = name;
    }

    public String getName() {
        return _name;
    }

    public void setName(String name) {
        _name = name;
    }

    public List<Expression> getArguments() {
        return _arguments;
    }

    public void setArguments(List<Expression> arguments) {
        _arguments = arguments;
    }

    public void addArgument(Expression arg) {
        if (_arguments == null) {
            _arguments = new ArrayList<Expression>();
        }
        _arguments.add(arg);
    }

    public Expression evaluate(EvaluationContext context) {
        StringBuilder buf = new StringBuilder(getName());
        buf.append("(");
        for (Expression expression : getArguments()) {
            buf.append(expression.evaluate(context).toString());
        }
        buf.append(')');
        return new LiteralExpression(buf.toString());
    }

    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder(getName());
        buf.append('(');
        for (Expression arg : getArguments()) {
            buf.append(arg.toString());
        }
        buf.append(')');
        return buf.toString();
    }
}

@@ -1275,16 +1275,58 @@
 d(%22(%22);%0A
+        if (getArguments() != null) %7B%0A    
         
@@ -1376,32 +1376,36 @@
 ) %7B%0A            
+   
+ 
 buf.append(expre
@@ -1433,32 +1433,46 @@
 t).toString());%0A
+            %7D%0A
         %7D%0A      

Check for null argument list in FunctionExpression