# -*- encoding : utf-8 -*-
module MailHelper
  
  def mail_content(slug, lang, arguments = {})
    
    lang_site = LangSite.find_by_slug(lang)
    
    mail_content = MailContent.joins(:mail_type).where(:lang_site_id => lang_site.id, :mail_types => { :slug => slug}).first
    
    @new_site = true
    if mail_content.content_type == "blocs" and mail_content.block
      r = "<div class='render_block'>"+render(:partial => "public/blocks/block", :locals => {:block => mail_content.block})+render(:partial => "public/shared/render_block.html.haml")+"</div>"

        
    else
    
      if mail_content.message?
        r= mail_content.message
      elsif mail_content.mail_type.mail_type_reference
        r= mail_content.mail_type.mail_type_reference.mail_contents.find_by_lang_site_id(lang_site.id).message.to_s
      else
        r= ""
      end
      
      r = simple_format(r)
    
    end
    
    
    arguments.each_pair do |key, value|
     r = r.gsub(/\[#{key}\]/, value.to_s)
       
    end
     
    if mail_content.mail_template and mail_content.mail_template.template_html?
      template = mail_content.mail_template.template_html
      
      
      r = template.gsub(/\[contenu\]/, r)
      

      
      
      raw r
      
      

    else
      raw r
    end
    
    
    
  end
  
end