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_action :set_locale


  
  
  before_action :debug_translation
  
  before_action :get_sorting
  
  before_action :set_qi_order
  
  
  def get_sorting( column_default=nil, direction_default="desc")
    
    
    
    sorting_direction = %w[asc desc].include?(params[:direction]) ? params[:direction] : direction_default
    sorting_column = params[:column] ? params[:column] : column_default
    
    if sorting_direction and sorting_column
      @sorting = [sorting_column, sorting_direction]
    end
  end
  
  
  
  
  def set_qi_order
    params[:search] = params[:search] || {}
    
    session[:qi_sort_string] = session[:qi_sort_string] || {}
    
    if params[:qi_sort_string].to_s != "" and params[:qi_sort_string][:class]
      
      if current_admin
        if ap = current_admin.admin_preferences.where(:key => "sort_table_"+params[:qi_sort_string][:class]).first
          ap.value = params[:qi_sort_string][:list].to_s
          ap.save
        else
          current_admin.admin_preferences.create(:key => "sort_table_"+params[:qi_sort_string][:class], :value => params[:qi_sort_string][:list].to_s)
        end

  
       
      else
        session[:qi_sort_string][params[:qi_sort_string][:class]] = params[:qi_sort_string][:list].to_s
      end
      
    else
      
    end
    
    
    if params[:qi_sort_string].to_s != "" and params[:qi_sort_string][:nbr_colonnes].to_s != ""
      
      if current_admin
        if ap = current_admin.admin_preferences.where(:key => "sort_table_"+params[:qi_sort_string][:class]+"_nbr_col").first
          ap.value = params[:qi_sort_string][:nbr_colonnes].to_s
          ap.save
        else
          current_admin.admin_preferences.create(:key => "sort_table_"+params[:qi_sort_string][:class]+"_nbr_col", :value => params[:qi_sort_string][:nbr_colonnes].to_s)
        end
           
      else        
        session[:qi_sort_string][params[:qi_sort_string][:class]+"_nbr_col"] = params[:qi_sort_string][:nbr_colonnes].to_s
      end
      
    else
      
    end
    
    
    
    
  end
  
  
  def sort_by_sorting(var, default=nil)
    if @sorting
      klass = var.klass.to_s
    
      if eval(klass).valid_sort.include?(@sorting[0])
        if @sorting[0].split(",").size > 0
          order_text = ""
          i = 0
          @sorting[0].split(",").each do |s|
            
            order_text += "#{s} #{@sorting[1]}"
            order_text += "," if i < (@sorting[0].split(",").size - 1 )
            i+=1
          end
          var = var.order(order_text)
        else
          var = var.order("#{@sorting[0]} #{@sorting[1]}")
        end
      

      end
      
    elsif default
      var = var.order(default)
    end
    
    
    
    return var
    
    

  end
  
  def debug_translation
    if params[:debug_translation]
      if current_admin
       @debug_translation = true 
      end
    end
  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
  

  
  
  
  
  private
  
  
    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
    
    
    def boolean_search(object, attribute)
      to_eval = 'if params[:search][:'+attribute.to_s+'].to_s == "Oui"
        @'+object.klass.to_s.tableize+' = @'+object.klass.to_s.tableize+'.where(:'+attribute.to_s+' => true)
      elsif params[:search][:'+attribute.to_s+'].to_s == "Non"
        @'+object.klass.to_s.tableize+' = @'+object.klass.to_s.tableize+'.where(:'+attribute.to_s+' => false)
      end'
    
      eval to_eval

    end
    
    
    def text_search(object, attribute)
      to_eval = 'if params[:search][:'+attribute.to_s+'].to_s != ""
        @'+object.klass.to_s.tableize+' = @'+object.klass.to_s.tableize+'.where("'+attribute.to_s+' LIKE ?","#{params[:search][:'+attribute.to_s+']}%")
      end'
      
    
      eval to_eval

    end
    
    def collection_search(object, attribute)
      to_eval = ' 
      
      if params[:search][:'+attribute.to_s+'].to_s != ""
        if params[:search][:'+attribute.to_s+'].to_s == "null"
          @'+object.klass.to_s.tableize+' = @'+object.klass.to_s.tableize+'.where(:'+attribute.to_s+' => nil)
        else
          @'+object.klass.to_s.tableize+' = @'+object.klass.to_s.tableize+'.where(:'+attribute.to_s+' => params[:search][:'+attribute.to_s+'])
        end
      end
      
      
      '
      
    
      eval to_eval

    end
    
    
    
    
    
    
    
    
    
  
    
    
    helper_method :current_admin, :get_specific_pref, :sort_by_sorting, :has_permission?, :boolean_search
  
  
  
  
  
end

