class Api::OrdersController <  Api::ApiController

  #Description
  # This API show current order details
  # Input Params - order_id
  def show
      order = Order.find(params[:order_id])
      order.order_items
  end

   def get_order
      order = Order.find(params[:order_id])
      order.order_items
  end

  # API - This api will retrive current booking for android with table or room id
  def view_orders
    booking_id = params[:booking_id]
    table_id = params[:table_id]
    if Booking.exists?(booking_id)
      booking = Booking.find(booking_id)

      if booking
        if booking.dining_facility_id.to_i == table_id.to_i
          if booking.booking_status == 'assign'
             if booking.sale_id.nil?
               @booking = booking
             end
          end
        else
          table = DiningFacility.find(table_id)
          @booking = table.get_booking
        end
      end
    else
      table = DiningFacility.find(table_id)
      @booking = table.get_booking
    end
  end

  # Description
  # This API allow new order creation
  # Input Params
  # order_source [* default - emenu] | table_id | booking_id [table_booking_id & Room_booking_id] (*require for Dine-In) | order_type [* Default - Dine-in]
  # | guest_info (optional) | customer_id (* Default assigned to WALK-IN)
  # order_items {[item_code, item_instance_code , qty, option, variants]}
  # Output Params
  # Status [Success | Error | System Error] , order_id, error_message (*)
  def create
    Rails.logger.debug "Order Source - " + params[:order_source].to_s
    Rails.logger.debug "Table ID - " + params[:table_id].to_s

    # begin
    @order = Order.new
    @order.source = params[:order_source]
    @order.order_type = params[:order_type]
    @order.customer_id = params[:customer_id] == ""? "CUS-000000000001" : params[:customer_id] # for no customer id from mobile
    @order.items = params[:order_items]
    @order.guest = params[:guest_info]
    @order.table_id = params[:table_id]  # this is dining facilities's id
    @order.new_booking = true
    @order.employee_name = current_login_employee.name
      #Create Table Booking or Room Booking
      if !params["booking_id"].nil?
        # check booking id is already completed.
        booking = Booking.find(params[:booking_id])
        if booking
          if booking.dining_facility_id.to_i == params[:table_id].to_i && booking.booking_status != 'moved'
            if !booking.sale_id.nil?
              sale_status = check_order_with_booking(booking)
               # puts "WWwwWWWWWWww"
               # puts sale_status
              if sale_status
                return return_json_status_with_code(400, "bill requested")
              end
            else
              @order.new_booking = false
              @order.booking_id = params[:booking_id]
            end
          else
              sale_status = check_order_with_table(params[:table_id])
               # puts "OOOOOOOOO"
               # puts sale_status
              if sale_status
                return return_json_status_with_code(400, "bill requested")
              end
          end
        end #booking exists
      else
        sale_status = check_order_with_table(params[:table_id])
         # puts "MMMMMMMM"
         # puts sale_status
        if sale_status
          # return false , @message = "bill requested"
          return return_json_status_with_code(400, "bill requested")
        end
      end

     @status, @booking = @order.generate
  end

  # render json for http status code
  def return_json_status_with_code(code, msg)
    render status: code, json: {
      message: msg,
      booking_id: booking_id
    }.to_json
  end

  def check_order_with_table(table_id)
    table =  DiningFacility.find(table_id)
    if table
       booking = table.get_current_booking
       # puts booking
       if booking
        if !booking.sale_id.nil?
          if booking.sale.sale_status == "completed" || booking.sale.sale_status == "new"
           @order.new_booking = true
           return false
          end
        else
           @order.new_booking = false
           @order.booking_id = booking.booking_id
           return false
         end
       end
    end
  end

  # this can always true
  def check_order_with_booking(booking)
    if booking.sale.sale_status == "completed" || booking.sale.sale_status == "new"
      @order.new_booking = true
      return false
    else
      @order.new_booking = false
      @order.booking_id = params[:booking_id]
      return false
    end
  end
  # Description
  # This API - allow order to add new items to existing orders, does not allow you to remove confirm items
  # Update customer info, Guest Info
  # Input Params
  # order_id , order_items {[item_code, item_instance_code , qty, option, variants]}
  def update

  end

  def order_params
    params.permits(:order_source, :booking_id,:order_type,
                    :customer_id,:guest_info, :table_id, :room_id,
                    order_items: [:item_code, :qty, :option, :remark])
  end
end
