class Origami::OrderReservationController < BaseOrigamiController

  def index
  	@order = OrderReservation.latest_order #.active
    @count_on_order = OrderReservation.get_count_on_order
    @count_on_completed = OrderReservation.get_count_on_completed
    @receipt_bill = check_receipt_bill
  end

  def update
  	@id     = params[:order_id]
    @status = params[:status]
    min_type = params[:min_type]
    if min_type != ""
      expected_waiting_time = params[:expected_time]
    else
      expected_waiting_time = min_type + " " +params[:expected_time]
    end
    remark = params[:remark]
  	access_code = params[:access_code]
    receipt_bill = check_receipt_bill

    order_reserve = OrderReservation.find_by_transaction_ref(params[:ref_no])
    if !order_reserve.nil?
      if !ShiftSale.current_shift.nil?
        if @status == "processed"
          if order_reserve.status == "accepted"
            result = OrderReservation.create_doemal_order(order_reserve,current_user)
          else
            Rails.logger.debug "Order reservation :: " + order_reserve.to_json + " :: send status ::" + @status
            result = {status: false, message: 'Could not send order to kitchen!'}
          end
        elsif @status == "delivery"
          if order_reserve.status == "send_to_kitchen"
            OrderReservation.update_order_reservation(@id, nil, "ready_to_delivery")
            response = OrderReservation.send_status_to_ordering(order_reserve.callback_url,order_reserve.transaction_ref,"ready_to_delivery")
            result = {:status=> true, :message => "ready for delivery" }
          else
            Rails.logger.debug "Order reservation :: " + order_reserve.to_json + " :: send status ::" + @status
            result = {status: false, message: 'Could not ready order to deliver!'}
          end
        elsif @status == "completed"
          if order_reserve.status == "ready_to_delivery"
            result = OrderReservation.update_doemal_payment(order_reserve,current_user,receipt_bill)
          else
            Rails.logger.debug "Order reservation :: " + order_reserve.to_json + " :: send status ::" + @status
            result = {status: false, message: 'Could not deliver order!'}
          end
        else
          if @status == "rejected"
            if order_reserve.status == "new" || order_reserve.status == "accepted"
              OrderReservation.update_order_reservation(@id, nil, "rejected",nil,remark,access_code,current_user)
              response = OrderReservation.send_status_to_ordering(order_reserve.callback_url,order_reserve.transaction_ref,"rejected")
              result = {:status=> true, :message => "rejected" }
            else
              Rails.logger.debug "Order reservation :: " + order_reserve.to_json + " :: send status ::" + @status
              result = {status: false, message: 'Could not reject order!'}
            end
          else
            if order_reserve.status == "new"
              OrderReservation.update_order_reservation(@id, nil, "accepted", expected_waiting_time)
              response = OrderReservation.send_status_to_ordering(order_reserve.callback_url,order_reserve.transaction_ref,"accepted")
              result = {:status=> true, :message => "accepted" }
            else
              Rails.logger.debug "Order reservation :: " + order_reserve.to_json + " :: send status ::" + @status
              result = {status: false, message: 'Could not accept order!'}
            end
          end
        end
      else
        result = { status: false, message: 'No current shift open for this employee!'}
      end
    else
      result = { status: false, message: 'There is no order for '+params[:ref_no]+'!'}
    end
		render :json => result.to_json
  end

  def get_order
    if (params[:id])
      @order = OrderReservation.find(params[:id])
      return @order
    end
  end

  def send_status
    order_reservation = OrderReservation.find_by_transaction_ref(params[:ref_no])
    if !order_reservation.nil?
      if !ShiftSale.current_shift.nil? || params[:status] == 'accepted' || (order_reservation.status == 'new' && params[:status] == 'rejected')
        response = OrderReservation.send_status_to_ordering(params[:url],params[:ref_no],params[:status],params[:waiting_time],params[:min_type],params[:reason])
      else
        response = { status: false, message: 'No current shift open for this employee!'}
      end
    else
      response = { status: false, message: 'There is no order for '+params[:ref_no]+'!'}
    end
    render :json => response
  end

  def get_order_info
    order_reservation = OrderReservation.where("status = 'new'").count()
    render :json => order_reservation
  end

  def check_receipt_bill
    receipt_bill = Lookup.collection_of("order_reservation")

    status = 0
    if !receipt_bill.nil?
      receipt_bill.each do |receipt|
        if receipt[0].strip.downcase == "receiptbill"
          status = receipt[1].to_i
        end
      end
    end
    Rails.logger.debug "@@@@ receiptbill checked ::"
    Rails.logger.debug status
    return status
  end

end
