class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  
  

  
  before_filter :set_locale


  
  before_filter :popups
  
  before_filter :debug_translation
  
  
  before_filter :generate_today_kaps
  
  
  def generate_today_kaps
    current_p_customer.generate_kaps_day(Date.today) if current_p_customer
  end
  
  
  
  def debug_translation
    if params[:debug_translation]
      if current_admin
       @debug_translation = true 
      end
    end
  end
  def popups

   
  end
  
  def get_public_layout
    "public"
   
      
  end
  

  def set_locale
    # if params[:locale] is nil then I18n.default_locale will be used
    #I18n.locale = params[:locale]#"fr" #
    
    I18n.locale = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug I18n.locale.to_s
  end
  
  def default_url_options(options={})
    { :lang => I18n.locale }  #
  end
  


  def auth_p_customer

    session[:devise_id] = params[:d] if params[:d]
    if !current_p_customer and !(current_admin and params[:admin])
      session[:before_auth_url] = request.url
      redirect_to new_public_p_customer_auth_path()
    else
      if !(current_admin and params[:admin])
        @current_p_customer.last_sign_in_at = DateTime.now
        @current_p_customer.save
      end
    end
  end
  

  def genererate_current_p_customer_sheet
    
    
  end
  
  def current_p_customer_sheet
    
    if current_p_customer
      if cookies[:current_p_customer_sheet_id] and p_customer_sheet = PCustomerSheet.where(:id => cookies[:current_p_customer_sheet_id]).first and p_customer_sheet.p_customer_id == current_p_customer.id
        
        return p_customer_sheet
      else
        p_customer_sheet = current_p_customer.p_customer_sheets.create(:state => "panier")
        cookies[:current_p_customer_sheet_id] = p_customer_sheet.id
        return p_customer_sheet
        
      end
    
    else
      nil
    end
    
    
  end
  
  def p_customer_validated
    if current_p_customer and current_p_customer.account_validated
      
    else
      if current_p_customer.particulars.first
        redirect_to edit_public_particular_path(current_p_customer.particulars.first)
      end
    end
  end
  
  private
    def current_p_customer
      if cookies[:p_customer_auth_token] and PCustomer.exists?(:token => cookies[:p_customer_auth_token])
        a_c = PCustomer.find_by_token(cookies[:p_customer_auth_token])
        if !a_c.lock
          @current_p_customer = a_c

        else
          cookies[:p_customer_auth_token] = nil
          nil
        end
      else
        nil
      end
    end
  
  
    def auth_admin
      if !current_admin
        redirect_to new_admin_admin_auth_path
      end
      
    
    end
  



    def current_admin
      
      if cookies[:admin_remember_token]
        if @current_admin = Admin.find_by_remember_token(cookies[:admin_remember_token]) 
          @current_admin = Admin.find_by_remember_token(cookies[:admin_remember_token]) 
        else
            cookies[:admin_remember_token] =nil
        end
        
      end
      
      
     
    end
    
   

    def get_specific_pref(key="")
      
      sp = SpecificPreference.where(:key => key).first
      
      if sp
        return sp.value
      else
        return ""
      end
      
    end
    
    def has_permission?(permission)
     if current_admin and current_admin.has_permission?(permission)  
       true
     else
       redirect_to "/admin"
       
     end
    end
    
    def is_super_admin
      if current_admin and current_admin.super_admin
        true
      else
        redirect_to "/admin"
      end
    end
    
    
    helper_method :current_admin, :get_specific_pref, :current_p_customer, :current_p_customer_sheet
  
  
end

