/*
 * 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 com.google.gson.annotations.Expose;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.IndexColumn;

/**
 *
 * @author themi
 */
@XmlAccessorType(XmlAccessType.FIELD)
@Entity
@Table(name = "SDMX_LOGIC_CONDITIONS")
public class LogicCondition implements Serializable{

    @Id
    @GeneratedValue
    @Column(name="ID")
    private int id;
    
    @Expose
    @Column(name="EXPRESSION", nullable=false)
    private String expression;
    
    @Expose
    @Column(name="DSD_NAME", nullable=false)
    private String dsdName;
    
    @Expose
    @Fetch(value = FetchMode.SUBSELECT)
    @JoinTable(
         name="SDMX_LC_COMMON_DIMENSIONS",
         joinColumns=@JoinColumn(name="ID")
    )
    @IndexColumn(name="idx")
    @CollectionOfElements(fetch = FetchType.EAGER)
    private List<String> commonDimensions;
    
    @Expose
    @Column(name="OBS_VAL_ATTR_NAME", nullable=false)
    private String obsValAttrName;
    
    @Transient
    private transient List<Param> params;

    public static final String DELIMITER = ";";
    public static final String FORMATER_REGEX = "\\+|-|\\*|\\/|";
    public static final String[] OPERATORS = {"+","-","*","/"};

    public LogicCondition() {
//        obsValAttrName = "OBS_VALUE";
    }

    public List<Param> getParams() {
        if(params == null)
            parseParams();
        return params;
    }
    
    public void parseParams(){
        formateExpressionString();
        params = new ArrayList<>();
        //find enclosed dimensions with their values in supplied expression
        Matcher m = Pattern.compile("\\[(.*?)\\]").matcher(getExpression());
        while(m.find()) {
           String[] split = m.group(1).split("=");
           params.add(new Param(split[0], split[1]));
        }
    }
    
    protected void formateExpressionString(){
        //delete all whitespaces
        this.expression = expression.replaceAll("\\s", "");
    }

    public List<String> getCommonDimensions() {
        return commonDimensions;
    }

    public String getDsdName() {
        return dsdName;
    }

    public String getExpression() {
        return expression;
    }

    public void setCommonDimensions(List<String> commonDimensions) {
        this.commonDimensions = commonDimensions;
    }

    public void setDsdName(String dsdName) {
        this.dsdName = dsdName;
    }

    public void setExpression(String expressionsString) {
        this.expression = expressionsString;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public String getObsValAttrName() {
        return obsValAttrName;
    }

    public void setObsValAttrName(String obsValAttrName) {
        this.obsValAttrName = obsValAttrName;
    }
    
}
