const { isGPS } = require('../middlewares/ws_authentication')
const gpsEventsController = require('../controllers/GPS_EventsController')

module.exports.gpsNamespace = () => {
  const io = global._io
  const gps = io.of('/gps')
  console.log('2');

  // gpss middlewares
  gps.use(isGPS)
  console.log('3');

  // attach events
  gps.on('connection', socket => {
    socket.join(socket.userId)
    gpsEventsController.getAllNotifs(socket)

    /// /////////////////////////////////////////////////////// handle connection error
    socket.on('connect_error', err => {
      console.log(err.message)
    })

    /// ////////////////////////////////////////////////////// events
    socket.on('gps:fetchNotifications', data => gpsEventsController.fetchNotifications(socket))
  })
}
