module Pageable
  extend ActiveSupport::Concern

  def page_to_sitemap_entries(controller, page)
    
    path = nil
    options = {}
    entries = []
    
    if page.resourceful?
      page.resource_class.find_each do |object|
        path = Rails.application.routes.url_helpers.url_for({ :only_path => true, :controller => controller, :action => page.name }.merge(page.resource_params(object)))
        options[:updated_at] =  object.updated_at if object.respond_to?(:updated_at)
        entries << {:path => path, :options => options}
      end    
    else
       path = Rails.application.routes.url_helpers.url_for( :only_path => true, :controller => controller, :action => page.name)
       entries << {:path => path, :options => {}}
    end
    
    entries
    
  end
  module_function :page_to_sitemap_entries

  def controller_sitemap_entries(controller)
    entries = []
    Pageable.controller_pages(controller).map do |name, page|
      entries += page_to_sitemap_entries(controller, page) 
    end
    entries    
  end
  module_function :controller_sitemap_entries

  #
  # Pageable.controller_pages("site/site")
  #
  def controller_pages(controller)
    controller_class = "#{controller}_controller".camelize.constantize
    controller_class.pages if controller_class.respond_to?(:pages)
  end
  module_function :controller_pages
  
  included do
    before_filter :load_current_page
  end
  
  module ClassMethods
   def page(name, path, options = {}, &block)
     
     page_definition_options = options.clone
     
     this_controller_name = "#{self.name.underscore}".gsub("_controller", "")
     uid = "#{this_controller_name}##{name}"
     parent_uid  = if parent_opt = options.delete(:parent)
     parent_opt = parent_opt.to_s
       if parent_opt.include?("#")
         parent_opt
       else
         "#{this_controller_name}##{parent_opt}"
       end
     end
     
     resource_type = options.delete(:resource).try(:to_s)
     
     i18n_scope  = "pages.#{self.name.underscore.split('/').last}##{name}"
     title       = options.delete(:title) || I18n.t("#{i18n_scope}.title", :default => "#{name}".humanize)   

     if Page.table_exists?
       
       page = unless Fullstack::Cms.config.localize_pages
         Page.find_or_create_by_uid(uid, :title => title, :name => name)
       else
         Page.find_or_create_by_uid_and_locale(uid, I18n.locale || Fullstack::Cms.config.default_locale, :title => title, :name => name)         
       end 
  
       if parent_uid
         parent = unless Fullstack::Cms.config.localize_pages
           Page.find_by_uid(parent_uid)
         else
           Page.find_by_uid_and_locale(parent_uid, I18n.locale || Fullstack::Cms.config.default_locale)         
         end 
       
         page.move_to_child_of(parent) unless page.parent == parent
       end
       
       if resource_type
         page.update_attribute(:resource_type, resource_type) unless page.resource_type == resource_type
       end
       
       if page.path != path
         page.update_attribute(:path, path)
       end
       
     end

     map(name, path, options = {}, &block)
     @pages ||= ActiveSupport::HashWithIndifferentAccess.new
     @pages[name] = page
   end
   
   def page_names
     @page_names ||= @pages.map {|name, page| page.name}
   end
   
   def pages
     @pages ||= ActiveSupport::HashWithIndifferentAccess.new
   end
   
 end

 def load_current_page
   @current_page.reload if @current_page = self.class.pages[action_name]
 end
    
end