/*
 * ParameterVal.java
 *
 * Jim Frankenberger
 * USDA-ARS, West Lafayette IN
 * jrf@purdue.edu
 *
 * Created on August 30, 2004, 10:11 AM
 */

package ex1;

import javax.swing.*;

/**
 * This class holds the data portion of a parameter. There is info that points to the meta-info and
 * also whether the parameter has been modified.
 *
 * @author  jrf
 */
public class ParameterVal {
    private final ParamDef info;
    private String value;
    private boolean modified;
  
    /**
     * This constructs a ParameterVal class
     *   @param p parameter meta-info
     *   @val value of the parameter
     */
    ParameterVal(ParamDef p, String val) {
        info = p;
        value = (val);
        modified = false;
    }
    
    /**
     *  Get the value of the parameter applying any conversions needed.
     *    @param altUnits true if value should be returned in alternate (english) units
     *    @return the value of the paraemter as a string
     */
    public String getVal(boolean altUnits) {
        ParamDef.ColumnType columnType = info.getType();
        if (columnType != ParamDef.ColumnType.CHOICE) {
            if (altUnits==false)
                return value;
            else
                return info.getAltValue(value);
        } else {
            // handle a choice list
            int ind;
            try {
                 ind = Integer.parseInt(value);
            }
            catch(NumberFormatException nfe) {
                String estr = "Error in number: " + "'" + value + "' for parameter " + info.name;
                ErrorSink.add(estr);
                //System.out.println(estr);
                ind = -999;
            }
            if (ind != -999) {
               // find index in list of choice ID's
               String pstr = info.findChoiceString(ind);
               if (pstr != null)
                   return pstr;
            }
        }
        
        return "???";
    }
    
    /**
     *   Get the value of the parameter without any processing, this would be what is stored in the xml file
     *     @return the value of the parameter 
     */
    public String getRawVal() {
        return value;
    }
    
    /**
     * Change the value of the parameter
     *   @param val the new value to assign
     *   @param altUnits true if value coming in is in alternate units and needs to be converted
	 * @return 
     */
    public boolean setVal(String val, boolean altUnits) {
        
        boolean rc = true;
        
        switch (info.getType()) {
            case FLOAT:  //float
                try {
                    Float.valueOf(val);
                } catch(NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(null,"You must enter a float value for this parameter",
                        "Invalid Float Number",JOptionPane.ERROR_MESSAGE);
                    rc = false;
                }
                break;
            case INTEGER:  //int
                try {
                    Integer.valueOf(val);
                } catch(NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(null,"You must enter an integer value for this parameter",
                        "Invalid Integer Number",JOptionPane.ERROR_MESSAGE);
                    rc = false;
                }
                break;
            case STRING:  //string
                break;
            case CHOICE: // choice list int
                break;
        }
        
        if (rc == false)
            return rc;
        
        String val2 = val;
        if (altUnits)
           val2 = info.getAltRevValue(val);
    
        if (info.getType() != ParamDef.ColumnType.CHOICE) {
            if (value.equals(val2) == false) {
                modified = true;
                value = (val2);
            }
        } else {
            // handle a choice list
            int inx = info.findChoiceVal(val2);
            if (inx >= -1) {
                String temp = (String.valueOf(inx));
                if (value.equals(temp) == false) {
                    modified = true;
                    value = temp;
                }
            }
        }
        return true;
    }
    
    /**
     *   Check id this parameter has been changed
     *     @return true if the value has changed, false otherwise
     */
    public boolean isModified() {
        return modified;
    }
    
    /**
     *  Get the name of the parameter
     *    @return the name of the parameter
     */
    public String getName() {
        return info.name;
    }
    
    /**
     *  Get the meta information about this parameter
     *     @return the information about the parameter
     */
    public ParamDef getInfo() {
        return info;
    }
    
    /**
     *  Get the actual name of the parameter
     *     @return the actual name of the parameter
     */
    public String getActualName() {
        return info.actualName;
    }
}

