class Api::OrderReserve::OrderReservationController < Api::ApiController
	# skip_before_action  :authenticate
  	ORDER = "order"
  	RESERVATION = "reservation"
  	ORDER_RESERVATION = "order_and_reservation"

	def check_customer
		customer_id  = nil
		status = false
		if !params[:name] || !params[:email] || !params[:contact_no] || !params[:membership_id]
			render :json => { :status => false, :message => "name, email, contact_no and membership_id are required!" }
		else
			status = true
		end

		if status
			check_customer = Customer.find_by_email_and_membership_id_and_customer_type(params[:email],params[:membership_id],'Doemal')
			if !check_customer.nil?
			  customer_id = check_customer.customer_id
			else
			  customer = OrderReservation.addCustomer(params)
			  customer_id = customer.id
			end
			if !customer_id.nil? 
				render :json => { :status => false, :message => "email and customer are already existed!"  }
			else
				render :json => { :status => true, :data => { :customer_id => customer_id} }
			end
		end
	end

	def create
		status = false
		if !params[:cus_info]
			result = { :status => false, :message => "cus_info is required!" }
		elsif !params[:requested_time]
			result = { :status => false, :message => "requested_time is required!" }
		elsif !params[:callback_url]
			result = { :status => false, :message => "callback_url is required!" }
		elsif !params[:reference]
			result = { :status => false, :message => "reference is required!" }
		elsif !params[:order_type]
			result = { :status => false, :message => "order_type is required!" }
		elsif params[:order_type] == ORDER || params[:order_type] == ORDER_RESERVATION
			if !params[:order_info]
				result = { :status => false, :message => "order_info is required!" }
			else
				status = true
			end
		elsif params[:order_type] == RESERVATION || params[:order_type] == ORDER_RESERVATION
			if !params[:reservation_info]
				result = { :status => false, :message => "reservation_info is required!" }
			else
				status = true
			end
		elsif !params[:payment_info]
			result = { :status => false, :message => "payment_info is required!" }
		elsif !params[:delivery_info]
			result = { :status => false, :message => "delivery_info is required!" }
		end

		if status == true
			order_reservation = params
			order_reservation_id, flag = OrderReservation.addOrderReservationInfo(order_reservation)

			if flag
				shop = Shop.find_by_id(1)
				if !shop.nil?
					shop_code = shop.shop_code
					order_audio = DisplayImage.find_by_shop_id_and_name(shop.id, "order_audio")
					if !order_audio.nil?
						audio = order_audio.image
					end
				else
					shop_code = ''
					audio = ''
				end

				result = { :status => true, :order_reservation_id => order_reservation_id, :message => "Order reservation is successfully created!" }
				order_reservation = OrderReservation.get_pending_orders #find(order_reservation_id)
				if ENV["SERVER_MODE"] == 'cloud'
			        from = "" #request.subdomain  + "." + request.domain
			    else 
			        from = ""
			    end
				ActionCable.server.broadcast "order_reservation_channel",data: order_reservation,shop_code: shop_code,from:from,audio:audio
			else
				result = { :status => true, :order_reservation_id => order_reservation_id, :message => "Order reservation is already existed!" }
			end
		end
		
		# puts "!!! send doemal ::"
		# puts result

		Rails.logger.debug "send status to doemal"
		Rails.logger.debug result
		render :json => result
	end

	def update_status
		order_reservation_id = params[:id]
		status = params[:status]
		remark = params[:remark]
		
		if params[:status] && params[:remark]
			reason = status +"||"+remark
			order_reservation = OrderReservation.find(order_reservation_id)

			if status == 'REMOVE'
				if order_reservation.status == "new" || order_reservation.status == "accepted"
					OrderReservation.update_order_reservation(order_reservation_id, nil, "rejected",nil,remark)
					result = {:status=> true, :message => "rejected" }
				else
					result = {:status=> false, :message => "Status :: "+order_reservation.status+" cann't remove!" }
				end
			elsif status == 'FOC'
				if order_reservation.status == "new" || order_reservation.status == "accepted" || order_reservation.status == "send_to_kitchen"
					OrderReservation.update_order_reservation(order_reservation_id, nil, nil,nil,reason)
					result = {:status=> true, :message => "FOC successfully" }
				else
					result = {:status=> false, :message => "Status :: "+order_reservation.status+" cann't get FOC!" }
				end
			elsif status == 'VOID'
				if order_reservation.status == "ready_to_delivery"
					OrderReservation.update_order_reservation(order_reservation_id, nil, nil,nil,reason)
					result = {:status=> true, :message => "VOID successfully" }
				else
					result = {:status=> false, :message => "Status :: "+order_reservation.status+" cann't VOID!" }
				end
			end
		else
			result = { :status => false, :order_reservation_id => order_reservation_id, :message => "status and remark is required!" }
		end

		render :json => result
	end

	def get_tax_profile
		tax_profiles = TaxProfile.where("group_type = 'doemal_order'")

		if !tax_profiles.nil?
			render :json => { :status => true, :data => tax_profiles }
		else
			render :json => { :status => false, :message => 'There is no record!' }
		end
	end
end
