/*
 *  Draco Econometrics
 *  Copyright (C) 2008-2009 Approximatrix, LLC
 *  http://www.approximatrix.com
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.approximatrix.draco.application.plugin;

public class PluginSimpleOption {

	public static final int INTEGER = 0;
	public static final int FLOAT = 1;
	public static final int STRING = 2;
	public static final int VARIABLE = 3;
	public static final int CONSTANT = 4;
	public static final int CHECKBOX = 5;
	public static final int RADIOBUTTONS = 6;
	
	public static Object GetDefaultOptionValue(int id) {
		switch(id) {
		case INTEGER:
			return new Integer(0);
		case FLOAT:
			return new Double(0.0);
		case STRING:
			return "";
		case VARIABLE:
			return "";
		case CONSTANT:
			return "";
		case CHECKBOX:
			return Boolean.TRUE;
		default:
			return "";
		}
	}
	
	public static String OptionId2String(int id) {
		switch(id) {
		case INTEGER:
			return "Integer";
		case FLOAT:
			return "Floating Point";
		case STRING:
			return "String";
		case VARIABLE:
			return "Variable Selection";
		case CONSTANT:
			return "Constant Selection";
		case CHECKBOX:
			return "Check Box";
		default:
			return "";
		}
	}
	
	public static String[] GetOptionStrings() {
		String[] list = new String[6];
		for(int i=0;i<list.length;i++)
			list[i] = OptionId2String(i);
		return list;
	}
	
	public static int OptionString2Id(String value) {
		int ret = 0;
		while(!OptionId2String(ret).equals(value) && OptionId2String(ret).length()>0)
			ret++;
		return ret;
	}
	
	private String label = null;
	private int type = INTEGER;
	private String value = null;
	
	public PluginSimpleOption(String label, int type, Object def_value) {
		this.label = label;
		this.type = type;
		if(!this.setDefaultObject(def_value))
			throw new IllegalArgumentException("Default value type was bad...");
	}
	
	public PluginSimpleOption(String label, int type, String def_value) {
		this.label = label;
		this.type = type;
		this.value = def_value;
	}

    public String getLabel() {
        return label;
    }

    public void setString(String value) {
        this.value = value;
    }
    
    public int getType() {
        return type;
    }
    
    public String getTypeString() {
    	return OptionId2String(type);
    }
    
    public String getString() {
        return value;
    }
    
    public boolean getBoolean() {
    	return new Boolean(value).booleanValue();
    }
    
    public int getInteger() {
        return getNumber().intValue();
    }
    
    public double getFloat() {
        return getNumber().doubleValue();
    }
    
    public Object getObject() {
    	switch(type) {
	    	case FLOAT:
	    	case INTEGER:
	    		return getNumber();
	    	case CHECKBOX:
	    		return new Boolean(getBoolean());
	    	default:
	    		return value;
    	}
    }
    
    public boolean setDefaultObject(Object value) {
    	if(value instanceof String) {
    		setDefaultValue((String)value);
    		return true;
    	}
    	
    	if(value == null) {
    		this.value = "";
    		return true;
    	}
    	
    	if(value instanceof String) {
    		this.value = value.toString();
    		return true;
    	}
    	
    	if(value instanceof Number) {
    		this.value = ((Number)value).toString();
    		return true;
    	}
    	
    	if(value instanceof Boolean && this.type == CHECKBOX) {
    		this.value = ((Boolean)value).toString();
    		return true;
    	}
    	
    	return false;
    }
    
    public void setDefaultValue(String value) {
    	switch(type) {
    	case FLOAT:
    		try {
    			this.value = new Double(value).toString();
    		} catch(NumberFormatException nfe) { }
    		break;
    	case INTEGER:
    		try {
    			this.value = new Integer(value).toString();
    		} catch(NumberFormatException nfe) { }
    		break;
    	case CHECKBOX:
    		if(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("t"))
    			this.value = "true";
    		else if(value.equalsIgnoreCase("false") || value.equalsIgnoreCase("f") || value == null)
    			this.value = "false";
    		break;
    	default:
    		if(value != null)
    			this.value = value;
    		break;
    	}
    }
    
    private Number getNumber() {
        try {
            switch(type) {
                case FLOAT:
                    return new Double(value);
                case INTEGER:
                    return new Integer(value);
                case CHECKBOX:
                    return getBoolean() ? new Integer(1) : new Integer(0);
                default:
                    return new Integer(0);
            }
        } catch(NumberFormatException nfe) {
            return new Integer(0);
        }    
    }
	
}
