package fanch.multijeu.numbers.data;

public class OperationExpr extends Expression {

    public boolean isEmpty() {
        return mFirst==null || mSecond==null;
    }

    public static enum Operation{
        ADD,
        SUB,
        MULT,
        DIV
    };

    protected Expression mFirst;
    protected Expression mSecond;
    protected Operation  mOperande;


    public OperationExpr(Operation op)
    {
        super();
        mOperande=op;
    }


    public OperationExpr(Operation op, Expression f, Expression s)
    {
        super();
        mFirst=f;
        mSecond=s;
        mOperande=op;
    }

    @Override
    public Expression clone() {
        OperationExpr ex = new OperationExpr(mOperande);
        if(mFirst!=null) ex.mFirst=mFirst.clone();
        if(mSecond!=null) ex.mSecond=mSecond.clone();
        return ex;
    }

    @Override
    public int opCount() {
        if(!isEmpty())
            return mFirst.opCount()+mSecond.opCount()+1;
        return 0;
    }

    @Override
    public String toString() {
        String out = "("+((mFirst!=null)?mFirst.toString():"");
        switch(mOperande)
        {
            case ADD:
                out+="+";
                break;
            case SUB:
                out+="-";
                break;
            case DIV:
                out+="/";
                break;
            case MULT:
            default:
                out+="*";
                break;
        }
        return out+((mSecond!=null)?mSecond.toString():"")+")";
    }

    @Override
    public int compute() {
        if(mSecond==null && mFirst!=null) return mFirst.compute();
        switch(mOperande)
        {
            case ADD:
                return mFirst.compute()+mSecond.compute();
            case SUB:
                return mFirst.compute()-mSecond.compute();
            case DIV:
                return mFirst.compute()/mSecond.compute();
            case MULT:
            default:
                return mFirst.compute()*mSecond.compute();
        }
    }

    @Override
    public Expression simplify() {
        return new NumberExpr(compute());
    }




    public Expression getFirst() {
        return mFirst;
    }

    public void setFirst(Expression mFirst) {
        this.mFirst = mFirst;
    }

    public Expression getSecond() {
        return mSecond;
    }

    public void setSecond(Expression mSecond) {
        this.mSecond = mSecond;
    }

    public Operation getOperande() {
        return mOperande;
    }

    public void setOperande(Operation mOperande) {
        this.mOperande = mOperande;
    }

}
