class Configuration

  attr_accessor :api_key,
                :secret_key,
                :api_url,
                :api_spec,
                :api_mode

  def initialize
    if defined?(Rails) and File.exist?(File.expand_path('config/cloudstack.yml', Rails.root))
      config = YAML.load_file(File.expand_path('config/cloudstack.yml', Rails.root))[Rails.env]

      @api_key = config['api_key'] if config['api_key'].present?
      @secret_key = config['secret_key'] if config['secret_key'].present?
      @api_url = config['api_url'] if config['api_url'].present?
      @api_mode = config['api_mode'] if config['api_mode'].present?
    end
    @api_spec = YAML.load_file(File.expand_path('../../config/api_spec.yml', __FILE__))

    # contain map of underscore api commands relative to camelize
    @method_to_command = Hash[ @api_spec.keys.map {|key| [key.underscore.to_sym, key]} ]
  end


  ##
  # return an array of api methods. Method is an underscored command.
  # it was done, because ruby methods should be underscored, but CloudStack
  # uses camelCased command names
  def methods
    @api_methods ||= @api_spec.keys.map{ |key| key.underscore.to_sym }
  end


  ##
  # return an array of available api commands. It uses in validations and so on.
  def commands
    @api_commands ||= @api_spec.keys
  end


  ##
  # convert underscored method name to camelCased command
  def method_to_command(command)
    @method_to_command[command.to_sym]
  end

  ##
  # return an array of given command parameters
  def command_params(command)
    @api_spec[command.to_s]['params'].keys.map(&:to_sym) if @api_spec[command.to_s]
  end

  ##
  # return an array of given command required parameters
  def command_required_params(command)
    @api_spec[command.to_s]['params'].map {|k,v| k.to_sym if v['required']}.compact
  end

end