'use strict';

require('dotenv').config();

const util = require('util');
const fs = require('fs');
const path = require('path');
const webPush = require('web-push');

webPush.setGCMAPIKey(process.env.GCM_API_KEY);


const NOTIFICATION_KEY = process.env.NOTIFICATIONS_KEY;
console.log("NOTIFICATION_KEY:", NOTIFICATION_KEY);
console.log("GCM_API_KEY:", process.env.GCM_API_KEY);

let storedPushCredentials = [];

/* TODO: SECURE DIAGNOSTIC ENDPOINT BEFORE HACKATHON */

exports.credentials = function(req, res) {
  res.json(storedPushCredentials);
}

exports.clearCredentials = function(req, res) {
  storedPushCredentials = [];
  res.json(storedPushCredentials);
}

/**
 * Publishes a notification
 */
exports.publish = function(req, res) {
  
    req.checkBody('apikey', 'API Key must not be empty').notEmpty();
    req.checkBody('apikey', 'API Key is incorrect').notificationKeyValid(NOTIFICATION_KEY);
    req.checkBody('text', 'Payload must not be empty').notEmpty();

    const errors = req.validationErrors();
    if (errors) {
        res.status(400).send('There have been validation errors: ' + util.inspect(errors));
        return;
    }
  
  const pushPayload = {
    text: req.body.text || "Static server notification payload...",
    icon: req.body.icon || req.protocol + '://' + req.get('host') + '/apple-touch-icon.png'
  }
  
  storedPushCredentials.forEach(function(pushCredentials, i) {
    
    webPush.sendNotification(pushCredentials, JSON.stringify(pushPayload))
    .then(function() {
      console.log("Push notification sent successfully");
    }).catch(function(error) {
      console.log(error);
    });
  
  }); //end forEach
  
  res.sendStatus(201);
};

/**
 * Subscribes a client to notifications
 */
exports.subscribe = function(req, res) {
  const newPushCredentials = {
    endpoint: req.body.endpoint,
    keys: {
      p256dh: req.body.p256dh,
      auth: req.body.auth
    }
  };
  
   const found = storedPushCredentials.some(function (element) {
    return element.endpoint === newPushCredentials.endpoint;
  });
  
  if (!found) {
    console.log("Pushing");
    storedPushCredentials.push(newPushCredentials);
  }
  
  res.sendStatus(200);
};
@@ -209,17 +209,16 @@
 _KEY);%0A%0A
-%0A
 const NO
@@ -268,114 +268,8 @@
 KEY;
-%0Aconsole.log(%22NOTIFICATION_KEY:%22, NOTIFICATION_KEY);%0Aconsole.log(%22GCM_API_KEY:%22, process.env.GCM_API_KEY);
 %0A%0Ale
@@ -628,19 +628,17 @@
  res) %7B%0A
-  
 %0A
+
     req.
@@ -1033,19 +1033,17 @@
 ;%0A    %7D%0A
-  
 %0A
+
   const 
@@ -1222,19 +1222,17 @@
 ng'%0A  %7D%0A
-  
 %0A
+
   stored
@@ -1290,13 +1290,9 @@
 ) %7B%0A
-    
 %0A
+
     
@@ -1508,19 +1508,17 @@
     %7D);%0A
-  
 %0A
+
   %7D); //
@@ -1529,19 +1529,17 @@
 forEach%0A
-  
 %0A
+
   res.se
@@ -1790,19 +1790,17 @@
  %7D%0A  %7D;%0A
-  
 %0A
+
    const
@@ -1923,19 +1923,17 @@
 ;%0A  %7D);%0A
-  
 %0A
+
   if (!f
@@ -2028,11 +2028,9 @@
   %7D%0A
-  
 %0A
+
   re
@@ -2048,10 +2048,11 @@
 s(200);%0A
-
 %7D;
+%0A

Remove console.log of environment vars