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

package uk.ac.reload.editor.scormadaptive.rules.jdom.rule;

import uk.ac.reload.editor.scormadaptive.rules.AdaptiveRules_EditorDialog;
import org.jdom.*;
import java.util.*;

/**
 * Class that contains the information corresponding to a 
 * ruleAction element of a PREEXISTING RULE
 *
 * @author  lagar
 * @version 0.2 
 */
public class ruleAction {
    
    private ArrayList<logicalExpression> oLEs = new ArrayList<logicalExpression>();
    
    private ArrayList<item> oItems = new ArrayList<item>();
    
    private ArrayList<organization> oOrganizations = new ArrayList<organization>();
    
    /** Creates a new instance of ruleAction */
    public ruleAction() {
        oLEs = null;
        oItems = null;
        oOrganizations = null;
    }
    
    public ruleAction(Element oEl) throws Exception {
        Element oElShow = oEl.getChild("show");
        
        if (oElShow == null) {
             throw new Exception("ruleAction: Element <show> is mandatory");
        }
        List oListLEs = oElShow.getChildren("logicalExpression");
        if (oListLEs == null) oLEs = null;
        else {
            Iterator oIt = oListLEs.iterator();
            while (oIt.hasNext()) {
                Element oElAux = (Element)oIt.next();
                add(new logicalExpression(oElAux));
            }
        }
        List oListItems = oElShow.getChildren("item");
        if (oListItems == null) oItems = null;
        else {
            Iterator oIt = oListItems.iterator();
            while (oIt.hasNext()) {
                Element oElAux = (Element)oIt.next();
                add(new item(oElAux));
            }
        }
        List oListOrgs = oElShow.getChildren("organization");
        if (oListOrgs == null) oOrganizations = null;
        else {
            Iterator oIt = oListOrgs.iterator();
            while (oIt.hasNext()) {
                Element oElAux = (Element)oIt.next();
                add(new organization(oElAux));
            }
        }
        
    }
    
    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 sTAB = AdaptiveRules_EditorDialog.sTAB;
        String sText = sTAB + "show";
        int i;
        
        for (i=0; i<oLEs.size(); i++) {
            sText += "(" + oLEs.get(i).getText() + ")";
//            System.out.println("--1> " + sText);
            if ((i < oLEs.size()-1) || (oItems.size() > 0) || (oOrganizations.size() > 0)) {
                sText += " and ";
            }
            //else sText += ")";
        }
        for (i=0; i<oItems.size(); i++) {
            sText += "(" + oItems.get(i).getTitle() + ")";
            //sText += "(" + oItems.get(i).getID() + ")";
            if ((i < oItems.size()-1) || (oOrganizations.size() > 0)) {
                sText += " and ";
            }
            //else sText += ")";
        }
        for (i=0; i<oOrganizations.size(); i++) {
            sText += "(" + oOrganizations.get(i).getTitle() + ")";
            //sText += "(" + oOrganizations.get(i).getID() + ")";
            if (i < oOrganizations.size()-1) {
                sText += " and ";
            }
            //else sText += ")";
        }
        
        sText += ";";
        char[] cList = sText.toCharArray();
        String ruleText[] = new String[1];
        ruleText[0] = "";
        int lines = 0;
        int iAnt = 0;
        int iSum = 0;
            
        //Recorremos la cadena buscando donde terminan actividades
        //para introducir saltos de l�nea
        for (i=0; i<cList.length-1; i++){
            //Cuando encontramos ") " acaba una actividad
            if ((cList[i]==')')&&(cList[i+1]==' ')) {
                String[] sAux = ruleText;
                ruleText = new String[lines+iSum+1];
                for (int j=0; j<sAux.length; j++) ruleText[j] = sAux[j];
                if ((lines+iSum) > 0)ruleText[lines+iSum] = sTAB + sTAB + sTAB;
                for (int ii=iAnt; ii<=i+1; ii++) ruleText[lines+iSum] += cList[ii];
                
                //ruleText[lines+iSum] += "\n";
                iAnt = i+2;
                iSum++;
            }
            //Comprobamos si es el ultimo
            else if ((cList[i+1]==';')&&(i+1==cList.length-1)) {
                String[] sAux = ruleText;
                ruleText = new String[lines+iSum+1];
                for (int j=0; j<sAux.length; j++) ruleText[j] = sAux[j];
                if ((lines+iSum) > 0) ruleText[lines+iSum]= sTAB + sTAB + sTAB;
                for (int ii=iAnt; ii<=i+1; ii++) ruleText[lines+iSum] += cList[ii];
            }
        }
        return ruleText;
    }
}
