class Api::Foodcourt::PaymentsController < Api::ApiController

  def create
    SalePayment.transaction do
      if params[:sale_id] && params[:account_no]
        @sale = Sale.find_by_sale_id(params[:sale_id])
        @booking = @sale.booking
        if !@sale.nil?
          if @sale.sale_status == "new"
            if !params[:account_no].empty?
              @status, @message = send_account_paymal(@sale.grand_total, params[:account_no], @sale.receipt_no)
              if @status
                sale_payment = SalePayment.new
                status, @sale_payment, @membership_data = sale_payment.process_payment(@sale, current_login_employee, @sale.grand_total, "paymal",params[:account_no])

                if status == true && @membership_data["status"] == true

                  @booking.orders.each do |order|
                    oqs = OrderQueueStation.new
                    oqs.pay_process_order_queue(order.order_id, @booking.dining_facility_id)

                    assign_order = AssignedOrderItem.assigned_order_item_by_job(order.order_id)
                    ActionCable.server.broadcast "order_queue_station_channel",order: assign_order
                  end

                  #card_balance amount for Paymal payment
                  card_balance_amount, transaction_ref = SaleAudit.getCardBalanceAmount(params[:sale_id])

                  @status = true
                  @card_balance_amount = card_balance_amount
                  @transaction_ref = transaction_ref
                  @message = "Payment successful."
                else
                  @status = false
                  if @membership_data
                    @card_balance_amount = @membership_data["card_balance_amount"]
                    @message = @membership_data["message"]
                  else
                    @message = "Payment failed!"
                  end
                end
              end
            else
              @status = false
              @message = "Card No is required!"
            end
          else
            @status = false
            @message = "Already paid for '#{params[:sale_id]}'!"
          end
        else
          @status = false
          @message = "There is no sale for '#{params[:sale_id]}'!"
        end
      else
        @status = false
        @message = "Parameters missing! #{params[:sale_id]} #{params[:account_no]}"
      end
    end
  end

  private
  def send_account_paymal(amount, account_no, receipt_no)
    sale = Sale.find_by_receipt_no(receipt_no)
    @out = []
    action_by = current_login_employee.name
    @status = true
    @message = ""
    membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
    if membership_setting.gateway_url
      member_actions = MembershipAction.find_by_membership_type("get_account_balance")
      if member_actions.gateway_url
        @campaign_type_id = nil
        url = membership_setting.gateway_url.to_s + member_actions.gateway_url.to_s
        merchant_uid= member_actions.merchant_account_id
        auth_token = member_actions.auth_token.to_s
        membership_data = SalePayment.get_paypar_account_data(url,membership_setting.auth_token,merchant_uid,auth_token,account_no,amount,receipt_no)
        if membership_data["status"]==true
          remark = "Payment by account no Receipt No #{sale.receipt_no} | Sale ID #{sale.sale_id} | Transaction ref: #{membership_data[:transaction_ref]} | Reload amount #{membership_data[:reload_amount]} | Old Balance Amount #{membership_data[:old_balance_amount]} | DateTime : #{membership_data[:date]}"
          sale_audit = SaleAudit.record_audit_for_edit(sale.sale_id,current_login_employee.name, current_login_employee.name,remark,"PAYBYACCOUNT" )
        else
          remark = "Payment by account no Receipt No #{sale.receipt_no} | Sale ID #{sale.sale_id} | Remark : #{membership_data[:message]}"
          sale_audit = SaleAudit.record_audit_for_edit(sale.sale_id,current_login_employee.name, current_login_employee.name,remark,"PAYBYACCOUNT" )
        end
        @out = membership_data
        @status   = membership_data["status"]
        @message  = membership_data["message"]
      end
    else
      @status   = false
      @message  = "No gateway url!"
    end

    return @status, @message
  end
end
