class MOdrFile < ApplicationRecord
  belongs_to :m_odr_rep
  belongs_to :m_odr_file_type
  belongs_to :p_customer
  belongs_to :m_odr_place
  belongs_to :m_event
  
  has_many :mail_hists

  has_many :m_odr_file_products, :dependent => :destroy
  accepts_nested_attributes_for :m_odr_file_products, :allow_destroy => true

  has_many :m_odr_file_roulages, :dependent => :destroy
  accepts_nested_attributes_for :m_odr_file_roulages, :allow_destroy => true
    

  mount_uploader :file, OdrUploader
  
  validates :file, :presence => true
  
  
  before_create { generate_token() }
  
  validates :buy_at, :presence => true, :if => :buy_at_needed?
  
  def buy_at_needed?
    true if self.m_odr_file_type_id == 2
  end
  
  
  def new_nbr_primes
    self.m_odr_file_roulages.count if self.m_odr_file_type_id == 1
    
  end
  
  def new_nbr_primes_useds
    MOdrPrime.where(:m_odr_file_roulage_id => self.m_odr_file_roulages.ids).count if self.m_odr_file_type_id == 1
    
  end
  
  def new_nbr_primes_not_useds
    (self.new_nbr_primes - self.new_nbr_primes_useds) if self.m_odr_file_type_id == 1
  
    
  end
  
  
  after_save do
    #self.p_customer.generate_prime if self.p_customer
  end
  before_validation do
    if self.m_odr_file_type_id == 1
      nbr_primes = 0
      nbr_primes_useds = 0
      self.m_odr_file_roulages.each do |mofr|
        mofr.save
        nbr_primes += mofr.nbr_primes.to_i
        nbr_primes_useds += mofr.nbr_primes_useds.to_i
      end
      self.nbr_primes = nbr_primes # if !self.id or !self.nbr_primes
      self.nbr_primes_useds = nbr_primes_useds
      self.nbr_primes_not_useds = self.nbr_primes - nbr_primes_useds
      
    elsif self.m_odr_file_type_id == 2
      nbr_pneus = 0
      self.m_odr_file_products.each do |mofp|
        nbr_pneus += mofp.qte.to_i
      end

      self.nbr_pneus = nbr_pneus
      self.nbr_pneus_not_useds = self.nbr_pneus - self.nbr_pneus_useds.to_i
    end

  end


  def generate_token()
    begin
      self[:token] = SecureRandom.urlsafe_base64
    end while MOdrFile.exists?(:token => self[:token])
  end
    
  
  
  def rotate(degrees=90)
    versions = [self.file.path]
    
    versions.each do |v|
      image = Magick::ImageList.new(v)
      image = image.rotate(degrees)
      image.write(v)
    end
    
    self.file.recreate_versions!(:secure, :secure_thumb)
    
    
    self.updated_at = Time.now
    self.save
  end




   acts_as_sorting :fields => {
      :id => {:name => "Id", :reorder => true},
      :created_at => {:name => "Date de téléchargement", :reorder => true},
      :p_customer => {:name => "Utilisateur"},
      :m_odr_file_type => {:name => "Type de document"},
      :admin_ok => {:name => "Décision", :reorder => true},
      :reject_reason => {:name => "Raison du rejet", :reorder => true},
      :reject_reason_description => {:name => "Raison du rejet : commentaire", :reorder => true},
      :nbr_pneus => {:name => "Nbr pneus", :reorder => true},
      :nbr_pneus_useds => {:name => "Nbr pneus utilisés", :reorder => true},
      :nbr_pneus_not_useds => {:name => "Nbr pneus restants", :reorder => true},
      :nbr_primes => {:name => "Nbr primes", :reorder => true},
      :nbr_primes_useds => {:name => "Nbr primes utilisés", :reorder => true},
      :nbr_primes_not_useds => {:name => "Nbr primes restantes", :reorder => true},


      :actions => {:name => "Actions", :reorder => true},

    }




  def send_mail_type(slug, element = nil)
    if self.m_odr_file_type_id
      mail_hist = MailHist.auto_generate_mail(:fr, slug, self.p_customer.email, {:arguments => {:nbr => self.nbr_pneus }, :m_odr_file => self, :p_customer => self.p_customer, :element => self})
    else
      mail_hist = MailHist.auto_generate_mail(:fr, slug, self.p_customer.email, {:arguments => {:nbr => self.nbr_pneus }, :m_odr_file => self, :p_customer => self.p_customer, :element => self})
    end
  end

  
  
end
