const express = require('express')
const bodyParser = require('body-parser');
const request = require('request');

const app = express()
const port = 3000;

app.set('view engine', 'ejs')

app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());


app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})


app.get('/', (req, res) => {
    res.render('index', {predictions:null})
})

app.post('/predict', (req,res) => {
    request.post({url:'http://127.0.0.1:5000/predict',formData:{
        text:req.body.plot
    }}, function (error, response, body) {
        console.error('error:', error); // Print the error
        console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
        console.log('body:', body); // Print the data received
        console.log(JSON.parse(body).predictionText)
        // res.send(JSON.parse(JSON.parse(body).prediction)); //Display the response on the website
        res.render('predictions',{predictions:JSON.parse(JSON.parse(body).predictionText)})
      });      
})