/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.csu.sdmx.validation.model;

import cz.csu.sdmx.Utils;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author themi
 */
public class Expression {

    private LogicCondition lc;
    private String expression;
    private List<Obs> obsList;

    public Expression(LogicCondition lc) {
        this.lc = lc;
        this.expression = lc.getExpression();
        this.obsList = new ArrayList<>();
    }
    
    public void addObs(Param param, Obs obs){
        //replace placeholder dimension by value of observation value(Obs)
        obsList.add(obs);
        this.expression = expression.replace(param.toString(), obs.getAttr(lc.getObsValAttrName()).replace(",", "."));
    }
    
    public String getExpression() {
        return expression;
    }
    
    public boolean foundObsValues(){
        return obsList.size() == lc.getParams().size();
    }
    
    public ValidationError createNotValidError(String filename, int conditionID){
        List<String> lineNumbers = new ArrayList<>();
        for(Obs obs : obsList)
            lineNumbers.add(obs.getLineNumber());
        
        ValidationError error = new ValidationError();
        error.setType(ValidationError.ERROR);
        error.setFilename(filename);
        error.setLineNumber(Utils.join(lineNumbers, ","));
        error.setText("Expression ["+expression+"] is not right by condition with ID["+conditionID+"].");
        
        return error;
    }
}
