class Design
  include Mongoid::Document
  include Mongoid::Timestamps

  field :title
  field :author
  field :intro
  field :version

  has_many :pages
  
  embeds_one :layout
  embeds_one :default_css, :class_name => "Stylesheet" 
  embeds_one :reset_css, :class_name => "Stylesheet"
  embeds_many :stylesheets
  embeds_many :themes
  embeds_many :javascripts
  embeds_many :images
  embeds_many :custom_images, :class_name => 'Image', as: :design_image
  
  validates_presence_of :title
  validates_presence_of :author
  
  after_save :procs_embedded_objects
  # after_destroy :del_embedded_objects
 
   # validate do
   #   errors.add('atttribute1',  'error for 1') if files_with_duplicate?
   #   errors.add('atttribute2',  'error for 1') if files_with_noname?
   # end
 
 def files_with_duplicate?
   [self.javascripts, self.images,self.themes].each do |objects_hash|
     ary = objects_hash.collect{ |k|  k.file_identifier}
     ary.compact!
     # debugger
     return true if(ary.count!=ary.uniq.count)
   end
   false
 end
 
 def files_with_noname?
   [self.javascripts, self.images,self.themes].each do |objects_hash|
     objects_hash.each{ |k| 
       return true if(k.file_identifier.nil? && !k.to_save?)
        }
   end
   false
 end
  def new_files=(*attrs)
    attrs[0].map  do |key,items_ary|   #Loop by JSs,Themes,Imgs
        self.files=([items_ary, key])
    end
  end
 
  def javascripts=(*attrs)
    self.files = (attrs << 'javascripts')
  end
  
  def themes=(*attrs)
    self.files = (attrs << 'themes')
  end
  
  def images=(*attrs)
    self.files = (attrs << 'images')
  end
  
  # Update or create the attribute records
  def files=(attrs)
    case attrs.last
      when 'layout'
        files = self.layout.build
      else
        files = eval(attrs.last)
    end
    attrs[0].each do |a|
       
      if a[:id].blank? && !a[:file].blank?
        files.build(:file => a[:file], :to_save => true)
      else
        files.each do |file|
          if file.id.to_s == a[:id]
             file.to_destroy = a[:to_destroy]
          end
        end
      end
    end
  end
  
  protected
  
  def del_embedded_objects
    [[self.layout],self.themes, self.javascripts, self.images].each do |objects|
      objects.each do |object|
        object.destroy
      end
     end
  end
  
  def procs_embedded_objects
    [self.javascripts, self.images].each do |objects|
      objects.each do |object|
        process_object(object)
      end
    end
    if self.layout.to_save
      self.layout.to_save = false
      self.layout.save
    end
    [[self.default_css], [self.reset_css], self.themes].each do |objects|
      objects.each do |object|
        if object
          to_parse_url = object.to_save ? true : false
          process_object(object)
          object.parse_urls if to_parse_url
        end
      end
    end
    self.valid?
  end
  
  def process_object(object)
    if object.to_save
      if object.file_identifier.nil?
        new_object = self.send(object._type.downcase.pluralize).build(object.attributes)
        new_object.file = object.file
        object.destroy
        new_object.to_save = false
        new_object.save
      else
        object.to_save = false
        object.save
      end
    end
    if object.to_destroy
      object.destroy
    end
  end
  
end
