class Oqs::PrintController < ApplicationController
  authorize_resource :class => false
  # Print Order Item
  def print
    # if ENV["SERVER_MODE"] != "cloud" #no print in cloud server
      order_slim_pdf = Lookup.collection_of("print_settings") #print_settings with name:OrderSlimPdf
      printer = PrintSetting.all
      unique_code="OrderItemStarPdf"
      if !order_slim_pdf.empty?
        if !printer.empty?
          printer.each do |printer_setting|
            if printer_setting.unique_code == 'OrderItemPdf'
              unique_code="OrderItemPdf"
            elsif printer_setting.unique_code == 'OrderItemStarPdf'
              unique_code="OrderItemStarPdf"
            elsif printer_setting.unique_code == 'OrderItemSlimPdf'
              unique_code="OrderItemSlimPdf"
            elsif printer_setting.unique_code == 'OrderSetItemPdf'
              unique_code="OrderSetItemPdf"
            elsif printer_setting.unique_code == 'OrderItemCustomisePdf'
              unique_code="OrderItemCustomisePdf"
            elsif printer_setting.unique_code == 'OrderSetItemCustomisePdf'
              unique_code="OrderSetItemCustomisePdf"
            elsif printer_setting.unique_code == 'OrderItemSlimCustomisePdf'
              unique_code="OrderItemSlimCustomisePdf"
            end
          end
        end
      end

      assigned_item_id = params[:id]
      options = params[:options]
      assigned_item = AssignedOrderItem.find(assigned_item_id)
      assigned_items = AssignedOrderItem.where("item_code='" + assigned_item.item_code + "' AND " + "order_id='" + assigned_item.order_id + "'")

      # order queue stations
      oqs = assigned_item.order_queue_station
      order_item = OrderItem.where("order_id='#{assigned_item.order_id}' AND item_instance_code='#{assigned_item.instance_code}'").first()

      # Check Printed
      print_status = assigned_item.print_status == true ? " (Re-Print)" : ""

      # print when complete click
      print_settings = PrintSetting.find_by_unique_code(unique_code)
      order_queue_printer = Printer::OrderQueuePrinter.new(print_settings)
      filename, receipt_no, cashier_printer = order_queue_printer.print_order_item(print_settings,oqs, assigned_item.order_id, order_item.order_items_id, print_status, "", options )

      # update print status for completed same order items
      assigned_items.each do |ai|
        ai.print_status=true
        ai.save
      end

      if ENV["SERVER_MODE"] == "cloud"
        result = {
          :filepath => filename,
          :printer_model => print_settings.brand_name,
          :printer_url => print_settings.api_settings
        }
        # Mobile Print
        render :json => result.to_json
      end
    # end
  end

  # Print Order Details with booking id
  def print_order_summary
    # if ENV["SERVER_MODE"] != "cloud" #no print in cloud server
      order_slim_pdf = Lookup.collection_of("print_settings") #print_settings with name:OrderSlimPdf
      printer = PrintSetting.all
      unique_code="OrderSummaryPdf"
      if !order_slim_pdf.empty?
        if !printer.empty?
          printer.each do |printer_setting|
            if printer_setting.unique_code == 'OrderSummaryPdf'
              unique_code="OrderSummaryPdf"
            elsif printer_setting.unique_code == 'OrderSummarySlimPdf'
              unique_code="OrderSummarySlimPdf"
            elsif printer_setting.unique_code == 'OrderSummarySetPdf'
              unique_code="OrderSummarySetPdf"
            elsif printer_setting.unique_code == 'OrderSummaryCustomisePdf'
              unique_code="OrderSummaryCustomisePdf"
            elsif printer_setting.unique_code == 'OrderSummarySetCustomisePdf'
              unique_code="OrderSummarySetCustomisePdf"
            elsif printer_setting.unique_code == 'OrderSummarySlimCustomisePdf'
              unique_code="OrderSummarySlimCustomisePdf"
            end
          end
        end
      end

      assigned_item_id = params[:id]
      table_name = params[:table_name]
      assigned_item = AssignedOrderItem.find(assigned_item_id)
      assigned_items = AssignedOrderItem.where("item_code='" + assigned_item.item_code + "' AND " + "order_id='" + assigned_item.order_id + "'");

      # order queue stations
      oqs = assigned_item.order_queue_station

      # Check Printed
      print_status = assigned_item.print_status == true ? " (Re-Print)" : ""

      # get dining
      # dining = DiningFacility.find_by_name(table_name);
      # booking = Booking.find_by_dining_facility_id(dining.id)

      # Get Booking ID
      booking_id = BookingOrder.where("order_id='#{assigned_item.order_id}'").pluck(:booking_id)[0]

      # print when complete click
      print_settings = PrintSetting.find_by_unique_code(unique_code)
      order_queue_printer = Printer::OrderQueuePrinter.new(print_settings)
      filename, receipt_no, cashier_printer = order_queue_printer.print_booking_summary(print_settings,oqs, booking_id, print_status)

       # update print status for completed same order items
      assigned_items.each do |ai|
        ai.print_status = true
        ai.save
      end

      if ENV["SERVER_MODE"] == "cloud"
        result = {
          :filepath => filename,
          :printer_model => print_settings.brand_name,
          :printer_url => print_settings.api_settings
        }
        # Mobile Print
        render :json => result.to_json
      end
    # end
  end

end
