/*
 *  LogicalExpression.java
 * 
 *  RELOAD Editor - An IMS Metadata, Content Packaging and Learning Design tool 
 *  with ADAPTATION AT ACTIVITY LEVEL
 *
 *  Copyright (c) 2006 Miguel Fernandez Lores
 *
 *  Technical Contact:
 *  Miguel Fern�ndez Lores
 *  e-mail: tucias@hotmail.com
 *
 */

package uk.ac.reload.editor.scormadaptive.rules.syntax;

import java.util.ArrayList;
import org.jdom.Attribute;
import org.jdom.Element;

/**
 * Class that establishes a logical condition among
 * the activities to be shown
 *
 * @author  lagar
 * @version 0.2 
 */
public class LogicalExpression {
    
    private String sCombination;
    
    private String sOperator;
    
    private Integer iNumber;
    
    private Integer iChilds;
      
    private ArrayList<Activity> oActivitys; //item, organization or LogicalExpression
    
    /** 
     * Creates a new instance of LogicalExpression 
     */
    public LogicalExpression(String combination, int number, int childs, String operator, ArrayList<Activity> activitys) {
        sCombination = combination;
        sOperator = operator;
        iNumber = number; // -1 is is not Select Logical Expression
        iChilds = childs;
        
        oActivitys = activitys;
    }
    
     public LogicalExpression(String combination, int number, int childs, String operator, Activity activity) {
        sCombination = combination;
        sOperator = operator;
        iNumber = number; // -1 is is not Select Logical Expression
        iChilds = childs;
        
        if (oActivitys == null) oActivitys = new ArrayList<Activity>();
        oActivitys.add(activity);
    }
    
     public String getNameToShow() {
         String sName="";
         
         if ((sCombination == null)&&(sOperator != null)) {
             //En el caso del not, ponemos (not(activity))
             sName += sOperator + "(" + oActivitys.get(0).getTitle() +  ")";
         }
         else if (sCombination == "select") {
             sName += iNumber.toString() + "/" + iChilds.toString() + "(";
             for (int i=0; i<oActivitys.size(); i++) {
                 if (i == oActivitys.size()-1) sName += oActivitys.get(i).getTitle() + ")";
                 else sName += oActivitys.get(i).getTitle() + ", ";
             }
         }
         else {
             for (int i=0; i<oActivitys.size(); i++) {
                 if (i == oActivitys.size()-1) sName += "(" + oActivitys.get(i).getTitle() + ")";
                 else sName += "(" + oActivitys.get(i).getTitle() + ") " + sCombination + " ";
             }
         }
         return sName;    
     }

     /*
     public String getNameToShow() {
         String sName="";
         
         if ((sCombination == null)&&(sOperator != null)) {
             //En el caso del not, ponemos (not(activity))
             sName += sOperator + "(" + oActivitys.get(0).getID() +  ")";
         }
         else if (sCombination == "select") {
             sName += iNumber.toString() + "/" + iChilds.toString() + "(";
             for (int i=0; i<oActivitys.size(); i++) {
                 if (i == oActivitys.size()-1) sName += oActivitys.get(i).getID() + ")";
                 else sName += oActivitys.get(i).getID() + ", ";
             }
         }
         else {
             for (int i=0; i<oActivitys.size(); i++) {
                 if (i == oActivitys.size()-1) sName += "(" + oActivitys.get(i).getID() + ")";
                 else sName += "(" + oActivitys.get(i).getID() + ") " + sCombination + " ";
             }
         }
         return sName;    
     }
     */
     
     
    public Element validate(){
        Element oEL = new Element("logicalExpression");
        Attribute oAtt;
        
        if (sCombination == null){
            //Pongo AND por defecto
            oAtt = new Attribute("combination","and");
            oEL.setAttribute(oAtt);
        }
        else {
            oAtt = new Attribute("combination",sCombination);
            oEL.setAttribute(oAtt);
        }
        if (iNumber != -1) {
            oAtt = new Attribute("number",iNumber.toString());
            oEL.setAttribute(oAtt);
        } 
        if (sOperator == "not") {
            oAtt = new Attribute("operator",sOperator);
            oEL.setAttribute(oAtt);
        }
        
        for (int i=0; i<oActivitys.size(); i++){
            Activity oAct = oActivitys.get(i);
            switch (oActivitys.get(i).getType()) {
            case Activity.LOGICALEXP:
                for (int a=0; a<oAct.getLEArray().size(); a++){
                    //Recorremos todas las LE que componen esta activity
                    Element oELaux = oAct.getLE(a).validate();
                    oEL.addContent(oELaux);
                }
                break;
            case Activity.ORGANIZATION:
                Element oOrg = new Element ("organization");
                oAtt = new Attribute("IDRef",oAct.getID());
                oOrg.setAttribute(oAtt);
                oEL.addContent(oOrg);
                break;
            case Activity.ACTCHILD:
            case Activity.ACTWITHCHILD:
                Element oItem = new Element ("item");
                oAtt = new Attribute("IDRef",oAct.getID());
                oItem.setAttribute(oAtt);
                oEL.addContent(oItem);
                break;
            }
        }
        return oEL;
    }
    
    public int getActImplicadas(){
        int iActs = 0;
        if (oActivitys != null) {
            
            if (sCombination == "select") iActs = 1;
            else {
                for (int i=0; i<oActivitys.size(); i++) {
                    iActs += oActivitys.get(i).getActImplicadas();
                }
            }
            if (iActs == 0) iActs = 1;
           
        }
        return iActs;
    }
}
