class MailHist < ApplicationRecord
  belongs_to :mail_content
  belongs_to :mail_type
  belongs_to :m_odr_rep
  belongs_to :m_odr
  belongs_to :m_odr_prime
  belongs_to :lang_site
  belongs_to :m_odr_file

  belongs_to :p_customer
  
  belongs_to :element, :polymorphic => true
  
  acts_as_sorting :fields => { :created_at => {:name => "Date", :reorder => true},
      :p_customer => {:name => "Client"},
      :to_email => {:name => "Email dest.", :reorder => true},
      :subject => {:name => "Sujet.", :reorder => true},
      :mail_type => {:name => "Mail type"},
      :element => {:name => "Elément"},
      
      :from_email => {:name => "Email exp.", :reorder => true},
      
      :actions => "Actions"
    }
 

  def self.auto_generate_mail(lang_slug, mail_type_slug, email, options = {})
   

    
    mail_type = MailType.find_or_create(mail_type_slug)


    mail_hist = MailHist.generate_mail(lang_slug, mail_type, email, options)
    
  end
  
  
  def self.generate_mail(lang_slug, mail_type, email, options = {})
    
    #(lang_slug, mail_type, @m_odr_rep.particulars.first.email, {:arguments => {:numero_avoir => ""}, :m_odr => @m_odr})
    
    @options = options
    
    @lang_site = LangSite.find_by_slug(lang_slug)
    
    @mail_type = mail_type
    
    @mail_content = @mail_type.mail_contents.where(:lang_site_id => @lang_site.id).first
    
    @p_customer = options[:p_customer]
    @m_odr = options[:m_odr]
    @m_odr_rep = options[:m_odr_rep]
    @m_odr_file = options[:m_odr_file]
    @m_odr_prime = options[:m_odr_prime]
    @file_tunel = options[:file_tunel]
    
    @arguments = @options[:arguments]

    
    
    arguments = @arguments || {}
    
    if @p_customer and @p_customer.particular
      arguments[:civilite] = @p_customer.particular.civilite
      arguments[:nom] = @p_customer.particular.name
      arguments[:prenom] = @p_customer.particular.firstname
      
    end
    
    
    s = @mail_content.subject.to_s
    arguments.each_pair do |key, value|
     s = s.gsub(/\[#{key.to_s}\]/, value.to_s)
       
    end
    @subject = s
    
    r = @mail_content.message.to_s
    arguments.each_pair do |key, value|
     r = r.gsub(/\[#{key.to_s}\]/, value.to_s)
       
    end
    
    @body = r
    
    
    if @options[:from]
      @from = @options[:from] 
    elsif @m_odr and @m_odr.email_from?
      @from = @m_odr.email_from
    elsif @file_tunel and @file_tunel.from_email?
      @from = @file_tunel.from_email
    end
    
    
    
    @to_email = email
    
    mail_history = MailHist.create(:lang_site => @lang_site, :to_email => @to_email, :from_email => @from, :element => options[:element], :subject => @subject, :body => @body, :m_odr => @m_odr, :m_odr_rep => @m_odr_rep, :m_odr_prime => @m_odr_prime,:m_odr_file => @m_odr_file, :p_customer => @p_customer, :mail_content => @mail_content, :mail_type => @mail_type)
    
    
    mail = GeneralMails.general2(@to_email, @subject, @body, mail_options = {:m_odr => @m_odr, :file_tunel => @file_tunel})
    
    mail_history.subject_send = @subject
    mail_history.body_send = mail.body.encoded
    
    mail.deliver
    
    
    mail_history.save
    
    
      
    return mail_history
 
    
  end
  
  def deliver_mail
    mail = GeneralMails.general(self.lang_site.slug, self.mail_type.slug, self.email , self.subject, self.content)
    
    self.subject_cache = self.subject
    self.content_cache = mail.body.encoded
    
    mail.deliver
    
    self.sended = true
    self.sended_at = Time.now
    
    self.save
    
    
    
    
  end
  
end
