class Public::ProductOrdersController < ApplicationController

	layout "public"

  before_filter :auth_product_customer, :only => [:particulars, :particulars_save ]
  skip_before_filter :verify_authenticity_token, :only => [:payplug_ipn]
  def my_cart

  end

  def vourcher
    code = params[:vourcher_code]
    if vc = VourcherCode.find_by_slug( code)
      @product_order.vourcher_code = vc
      @product_order.save
      redirect_to :back
    else
      redirect_to :back, :notice => "Le code indiqué n'est pas valide"
      
      
    end
    
  end

  def particulars
    @product_order = ProductOrder.find(session[:product_order_id])
    if @product_order.paid
      redirect_to new_public_product_order_path(:paid => true)
    else
      @product_order.product_customer = current_product_customer
      @product_order.product_customer.particular_bill.skip_validation = true
      @product_order.product_customer.particular_send.skip_validation = true

      @product_order.save

      @product_order.product_customer.particular_bill.skip_validation = false
      @product_order.product_order_payment_type_ok_id = 3 if !@product_order.product_order_payment_type_ok_id?

    end

  end

  def particulars_save
    @product_order = ProductOrder.find(session[:product_order_id])
    @product_order.product_customer.particular_bill.skip_validation = false

    if @product_order.update_attributes(params.require(:product_order).permit!)

      @product_order.particular_bill = @product_order.product_customer.particular_bill.dup
      
      @product_order.particular_idem = @product_order.product_customer.particular_idem
      if @product_order.particular_idem
        @product_order.particular_send = @product_order.product_customer.particular_bill.dup
      else
        @product_order.particular_send = @product_order.product_customer.particular_send.dup
      end
      @product_order.save
      
			@product_order.archive_now
      
      
      redirect_to select_fdp_public_product_orders_path()
      
      
      
      

    else

      render :action => :particulars

    end

  end
  
  
  def select_fdp
    @product_order = ProductOrder.find(session[:product_order_id])
    @product_order.force_fdp_validation = true
    

  end
  
  
  def select_fdp_save
    
    @product_order = ProductOrder.find(session[:product_order_id])
    @product_order.force_fdp_validation = true
    if @product_order.update_attributes(params.require(:product_order).permit!)
       

        redirect_to select_payment_public_product_orders_path()

      
    else
      render :action => :select_fdp
    end
    
    
    
    
    
  end
  
  def select_payment
    @product_order = ProductOrder.find(session[:product_order_id])
    @product_order.archive_now
    if !@product_order.product_order_payment_type_ok_id?
      @product_order.product_order_payment_type_ok_id = 3
    end
    @product_order.force_payment = true
    

  end
  
  
  def select_payment_save
    
    @product_order = ProductOrder.find(session[:product_order_id])
    @product_order.archive_now
    @product_order.force_payment
    @product_order.force_fdp_validation = true
    @product_order.force_cgv_validation = true
    
    
    
    if @product_order.update_attributes(params.require(:product_order).permit!)
       
       
      if @product_order.product_order_payment_type_ok_id == 3
        redirect_to paid_by_payplug_public_product_orders_path()
      else
        if @product_order.product_order_payment_type_ok_id == 1
          @product_order_payment = ProductOrderPayment.create(:product_order_payment_type_id => @product_order.product_order_payment_type_ok_id)
          @product_order.product_order_payments << @product_order_payment
          @product_order.product_order_payment_ok = @product_order_payment
          
        end



        @product_order.completed_finish = true
        
        @product_order.save
        @product_order.after_paid
        ProductOrdersMails.confirmation_cheque(@product_order).deliver
        session[:product_order_id] = nil 
        redirect_to payment_instructions_public_product_order_path(@product_order.token)

      end
    else
      render :action => :select_payment
    end
    
    
    
    
    
  end
  
  
  def payment_instructions
    @product_order_ar = ProductOrder.find_by_token(params[:id])
  end
  
  
  
  def payplug_ipn
    
    
    payplug_retrieve_payement(params[:id])
    
    
    response = @response
    
    puts response["metadata"]
    @product_order = ProductOrder.find_by_token(response["metadata"]["product_order_id"])
    @product_order_payment = ProductOrderPayment.find_by_token(response["metadata"]["product_order_payment_id"])
    
    @product_order_payment.product_order_payment_type_id = 3
    @product_order_payment.amount = response["amount"].to_i*0.01
    @product_order_payment.payplug_payment_id = response["id"]
    
    if response["card"]
      @product_order_payment.payplug_card_id = response["card"]["id"]
      @product_order_payment.payplug_card_expiry_month = response["card"]["exp_month"]
      @product_order_payment.payplug_card_expiry_year = response["card"]["exp_year"]
      @product_order_payment.payplug_card_last_number = response["card"]["last4"]
      
    end
    
    if response["is_paid"] == true || response["is_paid"] == "true"
      
      @product_order_payment.paid_at = Time.now
      
      @product_order.paid = true
      @product_order.after_paid

      ProductOrdersMails.confirmation_cb(@product_order).deliver

      
    else
      @product_order.paid = false
    end
    @product_order.product_order_payment_ok = @product_order_payment
    @product_order.completed_finish = true
    @product_order.save
    @product_order_payment.save
    
    
    
    
    render :inline => "ok"
  end
    
    
  
  def paid_by_payplug
    @product_order = ProductOrder.find(session[:product_order_id])
    
    
    
    if @product_order.paid
      redirect_to select_payment_public_order_products_path(@product_order.id)
    else
      
      @product_order_payment = ProductOrderPayment.create(:product_order_payment_type_id => 3)
      @product_order.product_order_payments << @product_order_payment
      
      session[:product_order_payment_token] = @product_order_payment.token
      
      puts "AAAAAAAAAAAAAA"
      
      puts @product_order.particular_bill
      
      puts "AAAAAAAAAAAAAA"
      options = {
        :amount => @product_order.total_ttc.to_i,
        :email => @product_order.product_customer.email ,
        :first_name => @product_order.particular_bill.firstname,
        :last_name => @product_order.particular_bill.name,
        :return_url => "http://#{HOSTNAME}"+paid_by_payplug_confirm_public_product_orders_path(:token => @product_order_payment.token),
        :cancel_url => "http://#{HOSTNAME}"+select_payment_public_product_orders_path(),
        :notification_url  => "http://#{HOSTNAME}"+payplug_ipn_public_product_orders_path(),
        :product_order_id => @product_order.token,
        :product_order_payment_id => @product_order_payment.token,
        :save_card  => false,

      }
  
      payplug_init_payement(options)

      response = @new_payplug_payment

      @product_order_payment.payplug_payment_id = response["id"]
      @product_order_payment.save
      @product_order.save
    
      redirect_to response["hosted_payment"]["payment_url"]

    
    end
    
  end

  def paid_by_payplug_confirm
    
  
    @product_order_payment = ProductOrderPayment.find_by_token(params[:token])
    @product_order_ar = @product_order_payment.product_order
    
    payplug_retrieve_payement @product_order_payment.payplug_payment_id
    
    if @response and @response["is_paid"] == true# || response["is_paid"] == "true"
        @paid = true
        
        @product_order = ProductOrder.new
        session[:product_order_id] = nil
    else
        redirect_to select_payment_public_product_orders_path(:paid_fail => 1) 
    
    end
  
  
  end
    

end
