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

  def set_locale
    # if params[:locale] is nil then I18n.default_locale will be used
    #I18n.locale = params[:locale]#"fr" #
    I18n.locale = params[:locale] || "fr"
    
    
    if true
      
        begin
          http_url = "http://sideplace.com:8080/json/#{request.remote_ip}"
          
          http = Curl::Easy.http_get(http_url)

          result = JSON.parse(http.body_str)
          puts result

           if result and result["country_code"] and c = ISO3166::Country[result["country_code"]]
             
             result = Geocoder.search(c.translation("en")).each do |result|
               if result.country
                 @default_country = result
                 break
               end
             end
             
             
           end
       
         rescue
           puts "impossible de localiser le pays"
         end
      
      
      
      
      if @default_country and @default_country.geometry["location"] and @default_country.geometry["viewport"]
        session[:default_country] = {}
        session[:default_country][:lat] = @default_country.geometry["location"]["lat"]
        session[:default_country][:lng] = @default_country.geometry["location"]["lng"]
        session[:default_country][:radius] = Geocoder::Calculations.distance_between([@default_country.geometry["location"]["lat"],@default_country.geometry["location"]["lng"]], [@default_country.geometry["viewport"]["southwest"]["lat"],@default_country.geometry["viewport"]["southwest"]["lng"]], :units => :km).round
        session[:default_country][:country] = c.translation(I18n.locale.to_s)
        session[:default_country][:country_code] = c.alpha2
      else
        session[:default_country] = nil
      end
      
    end
    
    if session[:default_country] and !params[:place]
      
      params[:place] = session[:default_country][:country]
      params[:place_lat] = session[:default_country][:lat]
      params[:place_lng] = session[:default_country][:lng]
      params[:place_type] = "6"
      params[:place_country] = session[:default_country][:country]
      params[:with_radius] = "1"
      params[:place_dist] = session[:default_country][:radius]
      
      
    end
    
  end
  
  def default_url_options(options={})
    { :locale => I18n.locale }  #
  end
  
  
  def auth_annonce_account
    
    session[:devise_id] = params[:d] if params[:d] 
    if !current_annonce_account
      
      session[:before_auth_url] = request.url 
      redirect_to new_public_annonce_accounts_auth_path(:p => params[:p], :for_annonce => (true if params[:controller] == "public/annonces"), :for_mail => (true if params[:controller] == "public/annonce_accounts" and params[:action] == "mail"))
    end
      
    
  end
  
  
  private


  
  def auth_admin
    if !current_admin
      redirect_to new_admin_admin_auth_path
    end
      
    
  end
  
  
  private

    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 current_annonce_account
      if cookies[:annonce_account_auth_token] and AnnonceAccount.exists?(:token => cookies[:annonce_account_auth_token])
        a_c = AnnonceAccount.find_by_token(cookies[:annonce_account_auth_token])
        if a_c.not_blocked
          @current_annonce_account = a_c
        else
          cookies[:annonce_account_auth_token] = nil
          
          nil
        end
      else
        nil
      end
     
    end
    


  
    
    
    helper_method :current_admin, :current_annonce_account
  
end

