/*
 *  Criterion.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 Fernandez Lores
 *  e-mail: tucias@hotmail.com
 *
 */

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

import java.util.List;

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

public class Criterion {
    
    /*
     * 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 type of the criterion according to the definite 
     * constants initially of this class
     */
    private int iType;
    
    /**
     * Minimal allowed value if the criterion is of numerical type
     */
    private Integer iMin;
    
    /**
     * Maximal allowed value if the criterion is of numerical type
     */
    private Integer iMax;
    
    /**
     * List of valid values of the criterion in state type
     */
    private String[] oList;
    
    /**
     * Initialitates the class
     *
     * @param sName The name of the criterion
     * @param iType The type of the criterion
     * @param iMin Minimal value if the tyoe of the criterion is numeric. In opposite case NULL
     * @param iMax Minimal value if the tyoe of the criterion is numeric. In opposite case NULL
     * @param oList List of values (if is of type state) or of criterion componentes(if is of type COLLECTION)
     * @throws Exception If the name is NULL or empty
     */
    private void init(String sName,int iType,Integer iMin,Integer iMax,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.iMax= iMax;
        this.iMin = iMin;
        this.oList = oList;
        
    }
    
    /**
     * Integer type constructor
     *
     * @param sName The name of the criterion
     * @param iType The type of the criterion
     * @param iMin Maximal value
     * @param iMax Minimal value
     * @throws Exception If the the type is incorrect or the name is NULL
     */
    public Criterion(String sName,int iType, Integer iMin,Integer iMax) throws Exception {
        if (iType!=INTEGER) throw new Exception("This contructor is not suitable for this Criterion");
        init(sName,iType,iMin,iMax,null);
    }
    
    /**
     * State and Langcode type constructor
     *
     * @param sName The name
     * @param iType The type
     * @param oList The possible values
     * @throws Exception If the type is incorrect or oList is NULL or empty
     */
    public Criterion(String sName,int iType, String[] oList) throws Exception {

        if (iType!=STATE && iType!=LANGCODE) throw new Exception("This contructor is not suitable for this Parameter");
        if ((oList==null) || (oList.length==0)) throw new Exception("There are not values");
        init(sName, iType, null,null, oList);
    }
    
    /**
     * Complex type constructor
     *
     * @param sName The name
     * @param iType The type
     * @throws Exception If the type is incorrect
     */
    public Criterion(String sName,int iType) throws Exception {
        if (iType!=COMPLEX) throw new Exception("This contructor is not suitable for this Parameter");
        init(sName, iType, null,null, null);
    }
    
    /**
     * @return The type of the criterion
     */
    public int getType() {
        return iType;
    }
    
    /**
     * @return The minimal value for this criterion
     */
    public int getMin() {
        return iMin;
    }
    
    /**
     * @return The maximal value for this criterion
     */
    public int getMax() {
        return iMax;
    }
    
    /**
     * @return The values of the type STATE or LANGCODE
     */
    public String[] getValues() {
        if (iType == INTEGER) {
            String[] sValues = new String[getMax()-getMin()+1];
            for (int i=0; i<= getMax()-getMin(); i++) 
                sValues[i] = Integer.toString(getMin()+i);
            return sValues;
        }
        else if ((iType!=STATE) && (iType!=LANGCODE)) return null;
        return oList;
    }
    
    /**
     * @return The name
     */
    public String getName() {
        return new String(sName);
    }
    
}