/*
 * FunctionNode.java
 *
 * Created on 3. prosinec 2005, 13:40
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package cz.pcland.functions.nodes;
import cz.pcland.gavisualization.*;
import java.util.*;
import java.awt.*;
/**
 *
 * @author petr
 */
public class FunctionNode extends UnaryOperation{
    protected String functionName;
    
    /** Creates a new instance of FunctionNode */
    public FunctionNode(Node node, String fn) {
        super(node);
        this.functionName = fn;
    }
    
    public double evaluate(double varValue, String varName) {
        if(functionName.equalsIgnoreCase("SIN")){
            return Math.sin(child.evaluate(varValue, varName));
        }
        if(functionName.equalsIgnoreCase("COS")){
            return Math.cos(child.evaluate(varValue, varName));
        }
        if(functionName.equalsIgnoreCase("LOG")){
            //todo: to log10
            return Math.log(child.evaluate(varValue, varName));
        }
        if(functionName.equalsIgnoreCase("LN")){
            return Math.log(child.evaluate(varValue, varName));
        }
        if(functionName.equalsIgnoreCase("TG")){
            return Math.tan(child.evaluate(varValue, varName));
        }
        if(functionName.equalsIgnoreCase("SQRT")){
            return Math.sqrt(child.evaluate(varValue, varName));
        }
        if(functionName.equalsIgnoreCase("ABS")){
            return Math.abs(child.evaluate(varValue, varName));
        }
        return 0;
    }
    
   
    public String print(int priority) {
        return functionName + "(" + this.child.print(Priorities.LOWESTPRIORITY) + ")";
    }
    
    public String printSyntax(int priority) {
         return Operations.getColoredText(Color.blue, functionName) + "(" + this.child.printSyntax(Priorities.LOWESTPRIORITY) + ")";
    }
    
}
