# -*- encoding : utf-8 -*-
class Block < ApplicationRecord

  ContentTypes = {
    TitleContent: "Titre",
    TextContent: "Texte",
    ImageContent: "Image",
    LinkContent:"Lien",
    BreakContent: "Séparation",
    HtmlContent: "Code HTML",
    DownloadContent: "Téléchargement",
    GalleryContent: "Galerie photo / diaporama",
    DynamicContent: "Contenu dynamique",
    TableContent: "Tableau",
    BlockContent: "Bloc",
    MapContent: "Plan",
    QuoteContent: "Citation",
    MenuItemLinkContent: "Pavé lien",
    #TimelineHistoryContent: "Timeline",
    #PetitionContent: "Petition",
    VideoContent: "Vidéo",
    KeynumberContent: "Chiffre clef",
    ArticleindexsContent: "Index articles",
    AgirContent: "Bloc agir",
    TimeContent: "Bloc date",
    SlideContent: "Slider prédéfinie",
    ShareContent: "Ligne partage réseaux",
    
   }
  
   belongs_to :lang_site
  belongs_to :blockable, :polymorphic => true
  
  #has_many :portlets, :dependent => :destroy, -> {:order => :position}
  
  has_many :portlets,-> { order :position }, dependent: :destroy
  
  accepts_nested_attributes_for :portlets


  def allow_types(*type)
    if type == [:all]
      ContentTypes
    else
      allow_types = {}
      type.each do |content|
    
        allow_types[content] = ContentTypes[content]
      end
      allow_types
    end
  end

def alloweds_types
  allow_types :all #:TitleContent, :ImageContent
  
end

  def dup
    @block = Block.new(self.attributes)
    @block.id = nil
    @block.save
    
    self.portlets.each do |portlet|
      new_p = portlet.dup
      new_p.save
      @block.portlets << new_p
    end
    @block
  end

end
