package cnp.ew.grid;

import cnp.ew.properties.*;

public class CpFormulaBinaryOp extends CpFormulaExpression
{
    String op;

    CpFormulaExpression arg1;
    CpFormulaExpression arg2;

    public CpFormulaBinaryOp(String op, CpFormulaExpression arg1, CpFormulaExpression arg2)
    {
        this.op = op;
        this.arg1 = arg1;
        this.arg2 = arg2;
    }

    public Object value(CpGridModel grid)
    {
        // ToDo:  Should typecheck arguments are integer

        Object obj1 = arg1.value(grid);
        double double1;
        if (!(obj1 instanceof Number)) {
           // System.out.println("Arg1 not a number");
            double1 = 0;
        } else {
            double1 = ((Double)obj1).doubleValue();
        }
        Object obj2 = arg2.value(grid);
        double double2;
        if (!(obj2 instanceof Number)) {
         //   System.out.println("Arg2 not a number");
            double2 = 0;
        } else {
            double2 = ((Double)obj2).doubleValue();
        }

        if (op.equals("+")) {
            return new Double(double1 + double2);
        }
        if (op.equals("-")) {
            return new Double(double1 - double2);
        }
        if (op.equals("*")) {
            return new Double(double1 * double2);
        }
        if (op.equals("/")) {
            return new Double(double1 / double2);
        }
        return null;
    }

    public CpType getType()
    {
        return CpBasicType.NUMBER;
    }
}

