#  Draco Econometrics
#  Copyright (C) 2006-2008 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/>.

include Java

import com.approximatrix.draco.application.plugin.SimpleOptionList
import com.approximatrix.draco.application.plugin.PluginSimpleOption

module Plugin

    class PluginOptions 

        attr_reader :optionlist
    
        def initialize
            @optionlist = SimpleOptionList.new
        end
        
        def add(label, type, default_value)
            if(type.is_a? String)
                type.downcase!
                if(type[0..2] == 'int')
                    type = PluginSimpleOption.INTEGER
                elsif(type[0..2] == 'flo' or type[0..2] == 'dou')
                    type = PluginSimpleOption.FLOAT
                elsif(type[0..2] == 'str')
                    type = PluginSimpleOption.STRING
                elsif(type[0..4] == 'check' or type[0..3] == 'bool' or type[0..3] == 'true')
                    type = PluginSimpleOption::CHECKBOX
                elsif(type[0..2] == 'var')
                    type = PluginSimpleOption.VARIABLE
                elsif(type[0..2] == 'con')
                    type = PluginSimpleOption.CONSTANT
                else
                    type = PluginSimpleOption.STRING
                end
            end
            
            default_value = "" if default_value.nil?            

            @optionlist.add PluginSimpleOption.new(label,type,default_value.to_s)
            
        end

        def get_options
            ret = Hash.new
            @optionlist.each do |x|
                ret[x.getLabel] = get_option_value(x)
            end
            ret
        end
        
        def get_option(label)
            ret = nil
            @optionlist.each do |x|
                ret = get_option_value(x) if x.getLabel == label
            end
            ret
        end

        private
        
        def get_option_value(x)
            value = nil
            case x.getType
                when PluginSimpleOption::INTEGER
                    value = x.getInteger
                when PluginSimpleOption::FLOAT
                    value = x.getFloat
                when PluginSimpleOption::CHECKBOX
                    value = x.getBoolean
                else
                    value = x.getString
            end
            value        
        end
    
    end

end

