const express = require('express')
const app = express()
const sendSMS = require('./lib/Twilio.js')
require('dotenv').config()
const bodyParser = require('body-parser')
const methodOverride = require('method-override')

const {
  createTimer,
  startTimer,
  stopTimer,
  resetTimer,
  decrementTimer
} = require('./timers.js')

const {
  createAlert,
  editAlert,
  triggerAlert
} = require('./alerts.js')

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())

app.use(methodOverride());

let timersArray = []
let alertsArray = []

const updateTimer = timerId => {
  const timer = timersArray.filter(timer => timer.id === timerId)[0]
  const updatedTimer = decrementTimer(timer)
  timersArray = timersArray.map(t => {
    return (t.id === updatedTimer.id)
    ? updatedTimer
    : t
  })
}

// ## CORS middleware
// 
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
const allowCrossDomain = (req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
      
    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
      res.sendStatus(200);
    }
    else {
      next();
    }
};
app.use(allowCrossDomain);

app.post('/messages', (req, res) => {
  sendSMS(req.body.message)
  res.sendStatus(200)
})

app.get('/alert/:id', (req, res) => {
  const alert = alertsArray.filter(alert => alert.id === req.params.id)[0]
  res.send(alert)
})

app.post('/alert/:id/edit', (req, res) => {
  const alert = alertsArray.filter(alert => alert.id === req.params.id)[0]
  const editedAlert = editAlert(alert, req.body.newProps)
  alertsArray = alertsArray.map(a => {
    return (a.id === editedAlert.id)
    ? editedAlert
    : a
  })
  res.send(editedAlert)
})

app.post('/alert/:id/trigger', (req, res) => {
  const alert = alertsArray.filter(alert => alert.id === req.params.id)[0]
  const triggeredAlert = triggerAlert(alert)
  alertsArray = alertsArray.map(a => {
    return (a.id === triggeredAlert.id)
    ? editedAlert
    : a
  })
  res.send(triggeredAlert)
})

app.get('/alerts', (req, res) => {
  res.send(alertsArray)
})

app.post('/alerts/new', (req, res) => {
  const {description, triggerTime, timerId} = req.body
  const alert = createAlert(description, triggerTime, timerId)
  alertsArray = [...alertsArray, alert]
  res.send(alert)
})

app.get('/timer/:id', (req, res) => {
 const timer = timersArray.filter(timer => timer.id === req.params.id)[0] 
 res.send(timer)
})

app.get('/timers', (req, res) => {
  res.send(timersArray)
})

app.post('/timers/new', (req, res) => {
  const timer = createTimer(req.body.initialMinutes)
  timersArray = [...timersArray, timer]
  res.send(timer)
})

app.post('/timers/start', (req, res) => {
  const timer = timersArray.filter(timer => timer.id === req.body.id)[0]
  const startedTimer = startTimer(timer)
  const interval = setInterval(updateTimer, 1000, startedTimer.id)
  startedTimer.interval = interval
  timersArray = timersArray.map(t => {
    return (t.id === startedTimer.id)
    ? startedTimer
    : t
  })
  res.sendStatus(200)
})

app.post('/timers/stop', (req, res) => {
  const timer = timersArray.filter(timer => timer.id === req.body.id)[0]
  const stoppedTimer = stopTimer(timer)
  clearInterval(timer.interval)
  stoppedTimer.interval = null
  timersArray = timersArray.map(t => {
    return (t.id === stoppedTimer.id)
    ? stoppedTimer
    : t
  })
  res.sendStatus(200)
})

app.post('/timers/reset', (req, res) => {
  const timer = timersArray.filter(timer => timer.id === req.body.id)[0]
  const resetedTimer = resetTimer(timer)
  timersArray = timersArray.map(t => {
    return (t.id === resetedTimer.id)
    ? resetedTimer
    : t
  })
  res.sendStatus(200)
})

app.listen(process.env.PORT, () => {
  console.log('Example app listening on port ' + process.env.PORT)
})
@@ -2614,239 +2614,24 @@
 app.
-get('/timers', (req, res) =%3E %7B%0A  res.send(timersArray)%0A%7D)%0A%0Aapp.post('/timers/new', (req, res) =%3E %7B%0A  const timer = createTimer(req.body.initialMinutes)%0A  timersArray = %5B...timersArray, timer%5D%0A  res.send(timer)%0A%7D)%0A%0Aapp.post('/timers
+post('/timer/:id
 /sta
@@ -2704,36 +2704,38 @@
 imer.id === req.
-body
+params
 .id)%5B0%5D%0A  const 
@@ -3020,17 +3020,20 @@
 ('/timer
-s
+/:id
 /stop', 
@@ -3101,36 +3101,38 @@
 imer.id === req.
-body
+params
 .id)%5B0%5D%0A  const 
@@ -3377,17 +3377,20 @@
 ('/timer
-s
+/:id
 /reset',
@@ -3467,20 +3467,22 @@
 === req.
-body
+params
 .id)%5B0%5D%0A
@@ -3623,32 +3623,32 @@
 er%0A    : t%0A  %7D)%0A
-
   res.sendStatus
@@ -3653,24 +3653,242 @@
 us(200)%0A%7D)%0A%0A
+app.get('/timers', (req, res) =%3E %7B%0A  res.send(timersArray)%0A%7D)%0A%0Aapp.post('/timers/new', (req, res) =%3E %7B%0A  const timer = createTimer(req.body.initialMinutes)%0A  timersArray = %5B...timersArray, timer%5D%0A  res.send(timer)%0A%7D)%0A%0A
 app.listen(p

Refactor timer endpoints