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_image_var
  before_filter :set_order
  
  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"
    
    @lang = LangSite.find_by_slug I18n.locale.to_s
  end
  
  def default_url_options(options={})
    { :locale => I18n.locale }  #
  end

  def set_image_var
    @page_images_credits = []
  end

  def auth_inscrit
    if !current_inscrit
      redirect_to new_inscrits_auth_path
    end
  end

  def payplug_init_payement(options)

    api_key = CONFIG_PAYPLUG['api_key']

    host = "http://#{HOSTNAME}"

    @data = {
    :amount => (options[:amount]*100),
    :currency => "EUR",
    :customer =>{
      :email => options[:email],
      :first_name => options[:first_name],
      :last_name =>  options[:last_name]
    },
    :hosted_payment =>{
      :return_url => options[:return_url],
      :cancel_url => options[:cancel_url]
    },
    :notification_url =>  options[:notification_url],
    :metadata => {
      :product_order_id => options[:product_order_id],
      :product_order_payment_id => options[:product_order_payment_id]
     },
     :save_card => options[:save_card],
     :force_3ds => true
  }

    @c = Curl::Easy.new("https://api.payplug.com/v1/payments") do |curl|
      curl.headers["Authorization"] = "Bearer "+api_key
      curl.headers["Content-Type"] = "application/json"
      
      curl.verbose = true
    end


    @c.http_post(@data.to_json)

    @new_payplug_payment = JSON.parse(@c.body_str)

  end

  def payplug_retrieve_payement(id)
    api_key = CONFIG_PAYPLUG['api_key']
    host = "http://#{HOSTNAME}"

    @c = Curl::Easy.new("https://api.payplug.com/v1/payments/#{id}") do |curl|
      curl.headers["Authorization"] = "Bearer "+api_key
      curl.headers["Content-Type"] = "application/json"


      curl.verbose = true
    end



    @c.http_get()

    @response = JSON.parse(@c.body_str)
  end


  private



  def auth_admin
    if !current_admin
      redirect_to new_admin_admin_auth_path
    end


  end
  
  
  def auth_product_customer
    
    session[:devise_id] = params[:d] if params[:d] 
    if !current_product_customer
      
      session[:before_auth_url] = request.url 
      redirect_to new_public_product_customers_auth_path(:p => params[:p], :for_annonce => (true if params[:controller] == "public/annonces"), :for_mail => (true if params[:controller] == "public/product_customers" and params[:action] == "mail"))
    end
      
    
  end


  private
  
  def current_product_customer
    if cookies[:product_customer_auth_token] and ProductCustomer.exists?(:token => cookies[:product_customer_auth_token])
      a_c = ProductCustomer.find_by_token(cookies[:product_customer_auth_token])
      if !a_c.lock
        @current_product_customer = a_c
      else
        cookies[:product_customer_auth_token] = nil
        
        nil
      end
    else
      nil
    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 current_inscrit
      if session[:inscrit_id] and Inscrit.exists?(session[:inscrit_id])
        @current_inscrit = Inscrit.find(session[:inscrit_id])
      else
        nil
      end
    end

    def set_order
      if session[:product_order_id] && @product_order = ProductOrder.where(id: session[:product_order_id]).first
        
      else
        @product_order = ProductOrder.new
        
        @product_order.save
        
        session[:product_order_id] = @product_order.id
      end
    end

    helper_method :current_admin, :current_inscrit, :current_product_customer
end