class Api::PaymentsController < ActionController::API


  #Payment by Invoice ID
  # Payment Method - [Cash | CreditNote | VISA | MASTER | etc..]
  # Invoice No | Amount
  # Output
  # Status - [True/False] | Invoice | error_message (* when status false)
  def  create
    @invoice = Sale.find(params[:invoice_id])

    if (@invoice)
      handle_payment(@invoice)
    end
  end

  # Update of payment status from the external party
  # Invoice No | Payment ID | External params [] (* third party references and status)
  #
  def update

  end

  private
  def handle_payment(sale_payment)

    payment_method = params[:payment_method]
    sale_payment = SalePayment.new

    #:received_amount, :card_payment_reference, :vochure_no, :giftcard_no,
    #:customer_id, :external_payment_status
    case payment_method
      when "cash"
        sale_payment.payment_method = "cash"
        sale_payment.received_amount = params[:amount]
        @status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
      when "creditnote"
        sale_payment.payment_method = "creditnote"
        sale_payment.received_amount = params[:amount]
        sale_payment.customer_id = params[:customer_id]

        @status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
      when "visa"
        sale_payment.payment_method = "visa"
        sale_payment.received_amount = params[:amount]
        sale_payment.payment_reference = params[:payment_reference]

        @status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
      when "master"
        sale_payment.payment_method = "master"
        sale_payment.received_amount = params[:amount]
        sale_payment.payment_reference = params[:payment_reference]
        @status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
      when "jcb"
        sale_payment.payment_method = "jcb"
        sale_payment.received_amount = params[:amount]
        sale_payment.payment_reference = params[:payment_reference]
        @status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
      when "mpu"
        sale_payment.payment_method = "mpu"
        sale_payment.received_amount = params[:amount]
        sale_payment.payment_reference = params[:payment_reference]
        @status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
      when "unionpay"
        sale_payment.payment_method = "unionpay"
        sale_payment.received_amount = params[:amount]
        sale_payment.payment_reference = params[:payment_reference]
        @status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
      when "vochure"
        sale_payment.payment_method = "vochure"
        sale_payment.received_amount = params[:amount]
        sale_payment.customer_id = params[:customer_id]
        sale_payment.payment_reference = params[:vochure_no]
        @status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
      when "giftcard"
        sale_payment.payment_method = "giftcard"
        sale_payment.received_amount = params[:amount]
        sale_payment.customer_id = params[:customer_id]
        sale_payment.payment_reference = params[:giftcard_no]
        @status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
      when "paypar"
        sale_payment.payment_method = "paypar"
        sale_payment.received_amount = params[:amount]
        sale_payment.payment_reference = params[:payment_reference]
        #TODO: implement paypar implementation
        @status, @invoice = sale_payment.process_payment(sale_payment, current_login_employee.name)
    end
  end
end
