class Product < ActiveRecord::Base
  has_many :product_product_sizes, :dependent => :destroy
  has_many :product_sizes, :through => :product_product_sizes
  has_many :product_stocks, :dependent => :destroy
  accepts_nested_attributes_for :product_stocks

  has_many :product_langs, :dependent => :destroy
  has_many :product_images, :dependent => :destroy
  accepts_nested_attributes_for :product_langs

  scope :not_archived, -> { where(:archived => 0).order("position ASC") }
  
  validates :price_ht, :presence => true
  
  after_save do
    self.generate_stock
  end
  def in_stock
    product_stocks.where("stock_th > 0").count
  end
  def generate_stock
    ids_saveds = []
    ids_new_saveds = []
    
    if self.product_options.count > 0
      
      self.product_options.each do |product_option|
        if self.product_sizes.count > 0
          
          self.product_sizes.each do |product_size|
          
            if !(product_stock = self.product_stocks.where(:product_size_id => product_size.id, :product_option_id => product_option.id).first)
          
              product_stock = self.product_stocks.create(:product_size_id => product_size.id, :product_option_id => product_option.id, :stock_th => 1, :stock => 1)
          
              ids_new_saveds << product_stock.id
            end
        
            ids_saveds << product_stock.id
          
          end
    
        else
      
      
          if !(product_stock = self.product_stocks.where(:product_size_id => nil, :product_option_id => product_option.id).first)
          
            product_stock = self.product_stocks.create(:product_option_id => product_option.id, :stock_th => 1, :stock => 1)
          
            ids_new_saveds << product_stock.id
          end
        
          ids_saveds << product_stock.id
      
        end
        
      end
    
    else
      
      if self.product_sizes.count > 0
      
        self.product_sizes.each do |product_size|
          
          if !(product_stock = self.product_stocks.where(:product_size_id => product_size.id, :product_option_id => nil).first)
          
            product_stock = self.product_stocks.create(:product_size_id => product_size.id, :stock_th => 1, :stock => 1)
          
            ids_new_saveds << product_stock.id
          end
        
          ids_saveds << product_stock.id
          
        end
    
    
    
      else
        
        if !(product_stock = self.product_stocks.where(:product_size_id => nil, :product_option_id => nil).first)
          
          product_stock = self.product_stocks.create(:stock_th => 1, :stock => 1)
          
          ids_new_saveds << product_stock.id
        end
        
        ids_saveds << product_stock.id
        
      
      end
      
      
      
    end
    
    self.product_stocks.where("id not IN (?)", ids_saveds).destroy_all
    
  end
  
  def product_options_in_stock_id
    
    self.product_stocks.where("stock_th > 0").group(:product_option_id).map {|s| s.product_option_id}
  end
  
  def product_options_in_stock
    self.product_options.where(:id => self.product_options_in_stock_id)
  end
  
  
  def product_sizes_in_stock_id(product_option_id=nil)
    
    to_ids = self.product_stocks.where("stock_th > 0")
    to_ids = to_ids.where(:product_option_id => product_option_id) if product_option_id != :all
    to_ids.map {|s| s.product_size_id}
  end
  
  def product_sizes_in_stock(product_option_id=nil)
    self.product_sizes.where(:id => self.product_sizes_in_stock_id(product_option_id))
  end
  
  
  def tva
    0.0
  end

  def price_ttc
    price_ht+(price_ht*self.tva)
  end
  
  def price_reduced_ttc
    price_reduced_ht+(price_reduced_ht*self.tva)
  end
  
  def final_price_ht
    if price_reduced_ht?
      price_reduced_ht
    else
      price_ht
    end
  end
  
  def final_price_ttc
    final_price_ht+(final_price_ht*self.tva)
      
      
  end

  


  def lang(lang)
    self.product_langs.find_by_lang(lang)
  end

  has_many :product_options, :dependent => :destroy

  accepts_nested_attributes_for :product_options, :allow_destroy => true

  #belongs_to :default_image, :class_name => "ProductImage"


  def default_image
    self.product_images.order(:position).first
  end
    
  belongs_to :product_collection
  belongs_to :product_category

  before_validation do
    if self.product_collection_id

      if !self.product_category or !self.product_category.product_collections.where(:id => self.product_collection_id)

        self.product_collection_id = nil
      end
    end
  end
end
