/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package expressiontree.operators.binary;

import apfloatextraclasses.ApfloatMatrixException;
import apfloatextraclasses.ApfloatMatrixMath;
import expressiontree.ExpressionTreeElement;
import expressiontree.operands.Matrix;
import expressiontree.operands.NumberType;
import expressiontree.operators.BinaryOperator;
import expressiontree.operators.IncompatibleNumberFormatException;

/**
 * A kivonás operátorok osztálya.
 * @author Esta
 */
public class Subtraction extends BinaryOperator {

    @Override
    public ExpressionTreeElement getValue() throws IncompatibleNumberFormatException {
        ExpressionTreeElement op1 = this.operands.get(0).getValue();
        ExpressionTreeElement op2 = this.operands.get(1).getValue();
        if (op1 instanceof NumberType && op2 instanceof NumberType) {
            //ha számot találtunk, számot adunk vissza.
            NumberType a = (NumberType) op1;
            NumberType b = (NumberType) op2;
            NumberType retValue = new NumberType(this.precision,false);
            retValue.setValue(a.getNumericValue().subtract(b.getNumericValue()));
            return retValue;
        } else if (op1 instanceof Matrix && op2 instanceof Matrix) {
            //ha mátrixot találtunk, és a dimenziók egyeznek, ok.
            Matrix a = (Matrix) op1;
            Matrix b = (Matrix) op2;
            if (a.getCols() == b.getCols() && a.getRows() == b.getRows()) {
                try {
                    Matrix retValue = new Matrix(a.getRows(), a.getCols());
                    retValue.setValues(ApfloatMatrixMath.subtract(a.getMatrixValues(), b.getMatrixValues()));
                    return retValue;
                } catch (ApfloatMatrixException ex) {
                    throw new IncompatibleNumberFormatException(ex.getMessage());
                }
            } else { //rossz mátrix >> exception
                throw new IncompatibleNumberFormatException("ERROR_SUB");
            }
        } else { //EZ SOHA NEM FORDULHAT ELŐ
            throw new IncompatibleNumberFormatException("ERROR_SUB_UNKNOWN");
        }
    }

    @Override
    public String toText(boolean b) {
        if (b) {
            return "() - ()";
        } else {
            return "- ";
        }
    }

    @Override
    public String getLocalizedName() {
        return java.util.ResourceBundle.getBundle("languages/operators").getString("SUBTRACTION");
    }
}
