class FileTunelSend < ApplicationRecord
  
  include ActionView::Helpers::NumberHelper
  include ActionView::Helpers::TranslationHelper
  include ActionView::Helpers::UrlHelper
  
  has_many :file_tunel_downloads
  
  belongs_to :file_tunel
  
  before_create { generate_token() }
  
  def generate_token()
    begin
      self[:token] = SecureRandom.urlsafe_base64
    end while FileTunelSend.exists?(:token => self[:token])
  end
  
  
  
  
  QI_DYNAMICS = %w()

  eval(QI_DYNAMICS_CORE)
    
  def self.qi_table_order
    {
      :created_at => {:name => "Date", :reorder => true},
      :email => {:name => "Nom", :reorder => true},
      
      :lien => {:name => "Lien", :reorder => true},
      
      :actions => "Actions",
     
    }
    
  end
  
  def self.valid_sort
    r = []
    self.qi_table_order.each do |key, value|
      if value.instance_of? Hash
        if value[:reorder] and value[:reorder] == true
          r << key.to_s if value[:reorder]
        end
      end
    end
    return r
    
    
  end
  def send_mail_type(slug, element = nil)
   
    if self.file_tunel and mail_type = self.file_tunel.mail_types.where(:slug => slug).first 
      mail_hist = MailHist.generate_mail(:fr, mail_type, self.email, {:arguments => {:email => self.email, :lien => self.lien }, :element => self, :file_tunel => self.file_tunel})
    end
  end
  
  def lien
    Rails.application.routes.url_helpers.public_file_tunel_send_url(:id => self.token, :slug => self.file_tunel.slug, :format => :html)
    
  end
  
  
end
