/*
 *  ComplexCriterion.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.criterion;

import java.util.ArrayList;
import java.util.List;

/**
 * This class contains all the information relative to a criterion 
 * of adaptation at activity level
 *
 * @author  lagar
 * @version 0.2
 */

public class ComplexCriterion {
    
    /*
     * These constants are in use for identifying the type of the criterion
     */
    final public static int INTEGER=0;
    final public static int STATE=4;
    final public static int COMPLEX=5;
    final public static int LANGCODE=6;
    
    /**
     * The name of the criterion
     */
    private String sName;
       
    /**
     * The prefix used in this Complex Criterion
     */
    private String sPrefix;
    
     /**
     * The type of the criterion according to the definite 
     * constants initially of this class
     */
    private int iType;
    
    /**
     * List of valid values for the criterion
     */
    private String[] oList;
    
    /**
     * List of criterion <= Complex type
     */
    private ArrayList<Criterion> oCriterion;
    
    /**
     * Initialitates the class
     *
     * @param sName The name of the criterion
     * @param iType The type of the criterion
     * @param oList List of values (if is of type state)
     * @throws Exception If the name is NULL or empty
     */
    private void init(String sPrefix, String sName,int iType, String[] oList) throws Exception {
        if (sName==null || sName.equals("")) throw new Exception("Criterion must be named");
        this.sName = new String(sName);
        this.iType = iType;
        this.oList = oList;
        this.sPrefix = sPrefix;
        this.oCriterion = new ArrayList<Criterion>();
    }
    
    /**
     * State and Langcode type constructor
     *
     * @param sName The name
     * @param iType The type
     * @param oList The possible values
     * @throws Exception If oList is NULL or empty
     */
    public ComplexCriterion(String sPrefix, String sName,int iType, String[] oList) throws Exception {
        if ((oList==null) || (oList.length==0)) throw new Exception("There are not values");
        init(sPrefix, sName, iType, oList);
    }
    
    /**
     * Other Constructor
     * 
     * @param oCri Criterion object that contains initalitation data 
     */
    public ComplexCriterion(String sPrefix, Criterion oCri) throws Exception{
        init(sPrefix, oCri.getName(), oCri.getType(), oCri.getValues());
    }
    
    /**
     * @return The values of the type STATE or LANGCODE
     */
    public String[] getValues() {
     if ((iType!=STATE) && (iType!=LANGCODE)) return null;
        return oList;
    }
    
    /**
     * @return The prefix of the complex criterion
     */
    public String getPrefixComplex(){
        return (sPrefix + ".k");
    }
    
    /**
     * @return The prefix of the complex criterion
     */
     public String getPrefix(){
        return sPrefix;
    }
    
    /**
     * @return The type of the criterion
     */
    public int getType() {
        return iType;
    }
    
    /**
     * @return The values of the type STATE or LANGCODE
     */
    public ArrayList<Criterion> getChildCriteria() {
        return oCriterion;
    }
    
    /**
     * @return The name
     */
    public String getName() {
        return new String(sName);
    }
    
    /**
     * @return The size of the list of criterion used
     */
    public int getCriteriaSize(){
        if (oCriterion == null) return 0;
        return oCriterion.size();
    }
    
    
    public Criterion getCriterion(int iIndex) throws Exception{
        if (iIndex > oCriterion.size()) throw new Exception("Invalid index. Greater than ArrayList size");
        if (oCriterion == null) throw new Exception("There are not elements in the ArrayList");
        return oCriterion.get(iIndex);
    }
    
    public Criterion getCriterion(String sName) throws Exception{
        if (oCriterion == null) throw new Exception("There are not elements in the ArrayList");
        for (int i=0; i<oCriterion.size(); i++){
            if (oCriterion.get(i).getName().equals(sName)) return oCriterion.get(i);
        }
        return null;
    }
    
    public void addCriterion(Criterion oCri){
        if (oCriterion == null) oCriterion = new ArrayList<Criterion>();
        oCriterion.add(oCri);
    }

    public String[] getModelComplex() {
        String[] oSAux = new String[oCriterion.size()];
        
        if (oCriterion == null) return null;
        for(int i=0; i<oCriterion.size(); i++) oSAux[i] = oCriterion.get(i).getName();
        return oSAux;
    }
    
    public int getCriterionType(int iIndex){
        
        if ((oCriterion == null)||(iIndex >= oCriterion.size())) return -1;
        return oCriterion.get(iIndex).getType();
    }
    
    
}