/*
 *  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.jdom.rule;

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

/**
 * Class that establishes a logical condition among
 * the activities to be shown
 * For PREEXISTING RULES
 *
 * @author  lagar
 * @version 0.2 
 */
public class logicalExpression {
    
    private ArrayList<logicalExpression> oLEs;
    
    private ArrayList<item> oItems;
    
    private ArrayList<organization> oOrganizations;
    
    private String sCombination;
    
    private String sNumber;
    
    private String sChilds;
    
    private String sOperator;
    
    /** 
     * Creates a new instance of LogicalExpression 
     */
    public logicalExpression(Element oEl) throws Exception {
        Attribute oAtt = oEl.getAttribute("combination");
        int iChilds = 0; //para el Select
        if(oAtt == null) {
            throw new Exception("logicalExpression: Attribute combination is mandatory");
        }
        else sCombination = oAtt.getValue();
        
        oAtt = oEl.getAttribute("number");
        if(oAtt == null) sNumber = null;
        else sNumber = oAtt.getValue();
        oAtt = oEl.getAttribute("operator");
        if(oAtt == null) sOperator = "noOp";
        else sOperator = oAtt.getValue();
        
        List oListLEs = oEl.getChildren("logicalExpression");
        if (oListLEs == null) oLEs = null;
        else {
            Iterator oIt = oListLEs.iterator();
            while (oIt.hasNext()) {
                Element oElAux = (Element)oIt.next();
                iChilds++;
                add(new logicalExpression(oElAux));
            }
        }
        List oListItems = oEl.getChildren("item");
        if (oListItems == null) oItems = null;
        else {
            Iterator oIt = oListItems.iterator();
            while (oIt.hasNext()) {
                Element oElAux = (Element)oIt.next();
                iChilds++;
                add(new item(oElAux));
            }
        }
        List oListOrgs = oEl.getChildren("organization");
        if (oListOrgs == null) oOrganizations = null;
        else {
            Iterator oIt = oListOrgs.iterator();
            while (oIt.hasNext()) {
                Element oElAux = (Element)oIt.next();
                iChilds++;
                add(new organization(oElAux));
            }
        }
        sChilds = Integer.toString(iChilds);
    }
    
     public void add(logicalExpression oLE) {
        if (oLEs == null) {
            oLEs = new ArrayList<logicalExpression>();
            oLEs.add(oLE);
        }
        else oLEs.add(oLE);
    }
    
    public void add(item oItem) {
        if (oItems == null) {
            oItems = new ArrayList<item>();
            oItems.add(oItem);
        }
        else oItems.add(oItem);
    }
    
    public void add(organization oOrg) {
        if (oOrganizations == null) {
            oOrganizations =  new ArrayList<organization>();
            oOrganizations.add(oOrg);
        }
        else oOrganizations.add(oOrg);
    }
    
    public String getText() {
        String sText = "";
        int i;
        
        if (sCombination.equals("select")) {
            sText += sNumber + "/" + sChilds + "(";
            if (oLEs != null) {
                for (i=0; i<oLEs.size(); i++) {
                    sText += "(" + oLEs.get(i).getText() + ")";
                    if ((i < oLEs.size()-1) || (oItems != null) || (oOrganizations != null)) {
                        sText += ", ";
                    }
                    else sText += ")";
                }
            }
            if (oItems != null) {
                for (i=0; i<oItems.size(); i++) {
//                    sText += oItems.get(i).getID();
                    sText += oItems.get(i).getTitle();
                    if ((i < oItems.size()-1) || (oOrganizations != null)) {
                        sText += ", ";
                    }
                    else sText += ")";
                }
            }
            if (oOrganizations != null) {
                for (i=0; i<oOrganizations.size(); i++) {
//                    sText += oOrganizations.get(i).getID();
                    sText += oOrganizations.get(i).getTitle();
                                      if (i < oOrganizations.size()-1) {
                        sText += ", ";
                    }
                    else sText += ")";
                }
            }
        }
        else {
            if (oLEs != null) {
                for (i=0; i<oLEs.size(); i++) {
                    sText += "(" + oLEs.get(i).getText() + ")";
                    if ((i < oLEs.size()-1) || (oItems != null) || (oOrganizations != null)) {
                        sText += " " + sCombination + " ";
                    }
                }
            }
            if (oItems != null) {
                for (i=0; i<oItems.size(); i++) {
//                    sText += "(" + oItems.get(i).getID() + ")";
                    sText += "(" + oItems.get(i).getTitle() + ")";
                    if ((i < oItems.size()-1) || (oOrganizations != null)) {
                        sText += " " + sCombination + " ";
                    }
                }
            }
            if (oOrganizations != null) {
                for (i=0; i<oOrganizations.size(); i++) {
//                    sText += "(" + oOrganizations.get(i).getID() + ")";
                    sText += "(" + oOrganizations.get(i).getTitle() + ")";
                    if (i < oOrganizations.size()-1) {
                        sText += " " + sCombination + " ";
                    }
                }
            }
        }
        return sText;
    }
}