class Api::CheckInProcessController < Api::ApiController

	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])
				
				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")

				lookup_checkout_time = Lookup.collection_of("checkout_alert_time")
				alert_time_min = 0
				if !lookup_checkout_time.nil?
					if lookup_checkout_time[0][0] == 'min'
					  alert_time_min = (lookup_checkout_time[0][1]).to_i
					else
					  alert_time_min = 15
					end
				end

				render :json => { :status => true, :check_in_time => check_in_time, :check_out_time => check_out_time, :alert_time_min => alert_time_min }
			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")

				checkout_at = Time.now.utc

				if !lookup_checkout_time.nil?
					if lookup_checkout_time[0][0] == 'hr'
						checkout_at = checkout_at + (lookup_checkout_time[0][1]).to_i.hour
					else
						checkout_at = checkout_at + (lookup_checkout_time[0][1]).to_i.minutes
					end
				end

				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
      			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
