class Api::CheckInProcessController < Api::ApiController
	# before_action  :authenticate
	def check_in_time
		if params[:dining_id]	
			dining_facility = DiningFacility.find(params[:dining_id])		
			booking = dining_facility.get_current_checkout_booking
			if !booking.nil?

				# DiningFacility.check_in_booking(params[:dining_id])

				table = DiningFacility.find(params[:dining_id])
    				#Send to background job for processing
   				if ENV["SERVER_MODE"] == 'cloud'
			        from = request.subdomain  + "." + request.domain
			      else 
			        from = ""
			      end
      			ActionCable.server.broadcast "check_in_booking_channel",table: table,from:from
				
				check_in_time = booking.checkin_at.utc.getlocal.strftime("%Y-%m-%d %H:%M")
				check_out_time = booking.checkout_at.utc.getlocal.strftime("%Y-%m-%d %H:%M")

				extra_minutes = (booking.checkout_at - booking.reserved_at) / 1.minutes

				lookup_checkout_time = Lookup.collection_of("checkout_alert_time")
				alert_time_min = 0
				if !lookup_checkout_time.empty?
					now = Time.now.utc
			        lookup_checkout_time.each do |checkout_time|
			          arr_time = checkout_time[0].split("-")
			          start_time = Time.parse(arr_time[0].strip).utc.strftime("%H:%M%p")
			          end_time = Time.parse(arr_time[1].strip).utc.strftime("%H:%M%p")
			          if start_time <= now && now <= end_time
			            alert_time_min = checkout_time[1].to_i
			          end
			        end
					render :json => { :status => true, :check_in_time => check_in_time, :check_out_time => check_out_time, :alert_time_min => alert_time_min, :extra_minutes => extra_minutes }
				else
					render :json => { :status => true }
				end
			else 
				render :json => { :status => false, :error_message => "No current booking!" }
			end
		end
	end

	def check_in_process
		if params[:dining_id]
			dining_facility = DiningFacility.find(params[:dining_id])
			if dining_facility.status == "available"
				lookup_checkout_time = Lookup.collection_of("checkout_time")
				
				if !lookup_checkout_time.empty?
					today = Time.now.utc.getlocal
					checkout_at = Time.now.utc.getlocal

					lookup_checkout_time.each do |checkout_time|
						arr_time = checkout_time[0].split("-")
						start_time = Time.parse(arr_time[0].strip).utc.getlocal.strftime("%H:%M%p")
						end_time = Time.parse(arr_time[1].strip).utc.getlocal.strftime("%H:%M%p")
						if start_time <= today.strftime("%H:%M%p") && today.strftime("%H:%M%p") <= end_time
							checkout_at = checkout_at + (checkout_time[1]).to_i.minutes
						end
					end

					if checkout_at.strftime("%Y-%m-%d %H:%M%p") > today.strftime("%Y-%m-%d %H:%M%p")
						dining_facility.status = "occupied"
						dining_facility.save!

						if dining_facility.type == "Table"
							type = "TableBooking"
						else
							type = "RoomBooking"
						end
						
						booking = Booking.create({:dining_facility_id => params[:dining_id],:type => type,
				                                :checkin_by=>current_login_employee.name,:checkin_at => Time.now.utc,:checkout_at =>checkout_at, :booking_status => "assign", :reserved_at => checkout_at, :reserved_by => current_login_employee.name })
						booking.save!

						render :json => { :status => true, :checkout_at => booking.checkout_at.utc.getlocal.strftime("%Y-%m-%d %H:%M") }
					else
						render :json => { :status => true }
					end
				else
					render :json => { :status => true }
				end
			else
      			error_message = "#{dining_facility.type} is not available!"
      			render :json => { :status => false, :error_message => error_message }
			end
		else
      		error_message = "dining_id is required!"
      		render :json => { :status => false, :error_message => error_message }
		end
	end

	def request_time
		if !params[:booking_id].nil? && !params[:time].nil?
			time = Time.parse(params[:time])
			booking = Booking.find(params[:booking_id])

			checkout_at = booking.checkout_at.utc

			hr = (time.strftime("%H").to_i).to_int
			min = (time.strftime("%M").to_i).to_int
			checkout_at = checkout_at + hr.hour + min.minutes

			booking.checkout_at = checkout_at
			booking.save!

			render :json => { :status => true, :checkout_at => booking.checkout_at.utc.getlocal.strftime("%Y-%m-%d %H:%M") }
		elsif !params[:booking_id].nil? && params[:time].nil? 
			error_message = "time is required!"
      		render :json => { :status => false, :error_message => error_message }
		elsif params[:booking_id].nil? && !params[:time].nil?
			error_message = "booking_id is required!"
      		render :json => { :status => false, :error_message => error_message }
		else
      		error_message = "booking_id and time are required!"
      		render :json => { :status => false, :error_message => error_message }
		end
	end

	private
	  def check_in_process_params
	    params.permit(:dining_id,:booking_id,:time)
	  end
end
