class TestingNine < ActiveRecord::Base
  acts_as_interface
  
  
  
	
  #Attributes:
  #:one, :two, :three

  def self.default(type = 'new')
    hash = {}
    case type
    when 'new'
      new(hash)
    when 'create'
      create(hash)
    end
  end

  def self.search(params)
    #one, two, three
    search_params = {}
    if params
      params.each do |p|
        if p[1] != ''
          search_params[p[0]] = p[1]
        end
      end
      where(search_params.collect{ |x| "#{x[0]} LIKE '%#{x[1]}%'" }.join(" AND ") )
    else
      all
    end
  end

  def self.home_page_opts
    changes = {
      :templates => {:list => 'full', :display => 'box'}
    }
    change_page_opts(changes)
  end
  
  def self.search_page_opts
    changes = {
      :query => 'search', 
      :templates => {:list => 'search', :display => 'list_item'},
      :page => 0
    }
    change_page_opts(changes)
  end
  
  def self.results_from_page_opts(opts)
        #Define queries here
        case opts[:query]
        when 'default'
          results = all
        when 'search'
          results = search(opts[:search])
        end
        size = results.size
        results = results.order("#{opts[:order][:attr]} #{opts[:order][:direction]}")
        results = results.retrieve(opts[:page].to_i, opts[:limit].to_i)
        {:results => results, :total_size => size, :total_pages => (size / opts[:limit].to_i).ceil + 1 }
      end
      
      def self.default_page_opts(list_template = 'full', display_template = 'box')
         {
          :screen => 'main', #which "Screen" to operate in
          :search => {}, #This can be replaced with :keys => "value" 
          :query => 'default', #a list of preselected queries
          :page => 0, #the page you are on
          :limit => 10, #the amount to retrieve.
          :show_more => false,
          :order => {
            :attr => 'created_at', #The attribute to order by
            :direction => 'ASC' #Ascending or descending
          },
          :templates => {
            :list => list_template,
            :display => display_template
          }
        }
      end
  

end
