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
  
  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)
    puts "TEST"
    puts options[:firstname]
    puts "TEST"
    api_key = "sk_live_cc6817ebd3eebc6970a65c881908121e"
  
    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 => {
      :donate_transaction_id => options[:donate_transaction_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"
      #@datacurl.datas["amount"] = @data
    
    
    
      curl.verbose = true
    end
  
 
    @c.http_post(@data.to_json)
  
    @new_payplug_payment = JSON.parse(@c.body_str)
    
    puts @new_payplug_payment
  end
  
  def payplug_retrieve_payement(id)
    api_key = "sk_live_cc6817ebd3eebc6970a65c881908121e"
    
    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
  
  
  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_inscrit
      if session[:inscrit_id] and Inscrit.exists?(session[:inscrit_id])
        @current_inscrit = Inscrit.find(session[:inscrit_id])
      else
        nil
      end
     
    end
    


  
    
    
    helper_method :current_admin, :current_inscrit
  
end

