class Item

  include Mongoid::Document
  include Mongoid::Timestamps
  
  field :name, :index => true
  field :full_name, :index => true
  field :i18n_variable_id, :type => BSON::ObjectId
  field :parent_id, :index => true
  field :parent_name
  field :position, :type => Integer
  field :is_published, :type => Boolean, :default => true, :index => true
 
  validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/
  validates :name, :exclusion => { :in => LIST[:forbidden_item_names] }
  validates_uniqueness_of :name, :scope => :parent_id
  validates_presence_of :name, :full_name, :position, :is_published
  
  belongs_to :parent, :class_name => "Item"
  has_many :children, :class_name => "Item", :as => 'parent'
  
  before_validation :setup_default_value
  
  # Destroy the i18n_variable
  def destroy_i18n_variable
    self.i18n_variable.destroy rescue nil
  end
  
  def self.find_by_name(item_name)
    Item.first(:conditions => { :name => item_name, :is_published => true })
  end
  
  # Get an array of ancestors
  def ancestors
    node, nodes = self, []
    nodes << node = node.parent while !node.parent.blank? rescue nil
    nodes.reverse
  end
  
  # Update or create the i18n_variable record
  def i18n_variable=(attr)
    if self.i18n_variable_id
      self.i18n_variable.update_attributes(attr) rescue nil
    else
      var = I18nVariable.new(attr.merge({:key => self.name, :document_class => self.class}))
      var.save
      self.i18n_variable_id = var.id
    end
  end
  
   # Get the i18n_variable
  def i18n_variable
    @i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
  end
  
  # Build the url from the array of ancestors
  def url
    urls = ancestors.map{ |a| a.name } << self.name
    urls.join("/")
  end
  
  protected
  
  def setup_default_value
    # Set the position value within the parent scope
    if self.position.blank?
      max_page = Item.where(:parent_id => self.parent_id).count
      self.position = (max_page)? max_page + 1 : 1
    end
    
    # Set the parent value
    self.parent_name = Item.first(:conditions => {:id => self.parent_id} ).name rescue nil
    
    # Build the full_name from the ancestors array
    full_node = self.ancestors.map{ |a| a.name }.push( self.name )
    # Remove root node if not root
    full_node.shift if full_node.size >= 2
    self.full_name = full_node.join("/")
  end
  
  # Enable the validation for parent_id
  def validates_presence_of_parent_id?
    true
  end
    
end
