//const path = require("path");
//const spacyNLP = require("spacy-nlp");
//console.log('checking: ' + path.join(__dirname, "node_modules", "spacy-nlp", "src", "start-io"));
//const spacyServer = require(path.join(__dirname, "node_modules", "spacy-nlp", "src", "start-io"));
//const serverPromise = spacyServer({ port: process.env.IOPORT });
const express = require('express');

const app = express();
app.use(express.static(__dirname + '/public')); // js, css, images

var http = require('http').createServer(app);
var io = require('socket.io')(http);

// app.js
require('./config/express')(app);
app.get('/', (req, res) => res.render('index'));


app.listen(3000);
console.log('listening at:', 3000);

/*
const cassandra = require('cassandra-driver');
const client = new cassandra.Client({
  contactPoints: ['127.0.0.1'],
  port: 9876,
  localDataCenter: 'datacenter1',
  keyspace: 'ks1'
});
*/

io.on('connection', (socket) => {
  console.log('a user connected');
  
  socket.on('client-loaded', data => {
    console.log('SERVER HMMMM....');
    socket.emit('proxy-client-loaded', result.rows, data.started);
  });
  
  socket.on('dbQuery', data => {
    //setResponse(data);
    console.log('CAUGHT', data);
    
    var startLookupDB = new Date().getTime();
    const query = "SELECT * FROM athletes WHERE name['display'] = '" + data.name + "' ALLOW FILTERING";
    const start = new Date().getTime();
    
    client.execute(query).then(result => {
      console.log(result.rows);
      console.log('Took ' + (new Date().getTime()-startLookupDB) + 'ms');
      socket.emit('dbResponse', result.rows, data.started);
    });
  });
});


http.listen(3001, () => {
  console.log('listening on *:3001');
});