const express = require('express');
const router = express.Router();
const mongoose = require('mongoose').set('debug', false);
const Subscription = mongoose.model('subscribers');
// const q = require('q');
const webpush = require('web-push');

const tools = require('../tools/general');

const {authenticate} = require('../middleware/authenticate');

const shared_consts = require('../tools/shared_nodejs');
const server_constants = require('../tools/server_constants');

const {User} = require('../models/user');

const globalTables = require('../tools/globalTables');

router.post('/', (req, res) => {
  const payload = {
    title: req.body.title,
    message: req.body.message,
    url: req.body.url,
    ttl: req.body.ttl,
    icon: req.body.icon,
    image: req.body.image,
    badge: req.body.badge,
    tag: req.body.tag,
  };

  Subscription.find({}, (err, subscriptions) => {
    if (err) {
      console.error(`Error occurred while getting subscriptions`);
      res.status(500).json({
        error: 'Technical error occurred',
      });

    } else {
      let parallelSubscriptionCalls = subscriptions.map((subscription) => {
        return new Promise((resolve, reject) => {
          const pushSubscription = {
            endpoint: subscription.endpoint,
            keys: {
              p256dh: subscription.keys.p256dh,
              auth: subscription.keys.auth,
            },
          };

          const pushPayload = JSON.stringify(payload);
          const pushOptions = {
            vapidDetails: {
              subject: process.env.VAPI_KEY_SUBJECT,
              privateKey: process.env.PRIVATE_VAPI_KEY,
              publicKey: process.env.PUBLIC_VAPI_KEY,
            },
            TTL: payload.ttl,
            headers: {},
          };
          webpush.sendNotification(
              pushSubscription,
              pushPayload,
              pushOptions,
          ).then((value) => {
            resolve({
              status: true,
              endpoint: subscription.endpoint,
              data: value,
            });
          }).catch((err) => {
            reject({
              status: false,
              endpoint: subscription.endpoint,
              data: err,
            });
          });
        });
      });
      q.allSettled(parallelSubscriptionCalls).then((pushResults) => {
        console.info(pushResults);
      });
      res.json({
        data: 'Push triggered',
      });
    }
  });
});



async function SendMsgTo(idapp, username, params) {

  return await User.find({idapp, username}).then(async (arrusers) => {
    if (arrusers !== null) {
      for (const user of arrusers) {
        await tools.sendNotificationToUser(user._id, params.title, params.content,
            params.openUrl, params.openUrl2, params.tag, params.actions, params.id).
            then(ris => {
              if (ris) {

              } else {
                // already sent the error on calling sendNotificationToUser
              }
            }).
            catch(e => {
              console.error(e.message);
            });
      }
    }
  });

}

router.post('/send', authenticate, async (req, res) => {
  const idapp = req.body.idapp;
  const params = req.body.params;

  // console.log('params', params);

  let nummsg = 0;

  if ((!User.isAdmin(req.user.perm) && !User.isManager(req.user.perm) &&
      !User.isFacilitatore(req.user.perm))) {
    // If without permissions, exit
    return res.status(404).
        send({code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: ''});
  }

  let msgcosa = 'da Inviare';
  if (params.sendreally) {
    msgcosa = 'Inviati';
  }

  try {
    if (params.typemsg === shared_consts.TypeMsg.SEND_TO_MYSELF) {
      params.usernameDest = req.user.username;
    }

    const ris = await globalTables.SendMsgToParam(idapp, params);

    return res.send({
      code: server_constants.RIS_CODE_OK,
      msg: ris.nummsgsent + ` Msg ${msgcosa} su ` + ris.numrec + ' !',
      nummsg: ris.nummsgsent,
    });
  } catch (e) {
    return res.send(
        {code: server_constants.RIS_CODE_ERR, msg: nummsg + ` Msg ${msgcosa} !`});
  }

});

router.get('/', (req, res) => {
  res.json({
    data: 'Invalid Request Bad',
  });
});
module.exports = router;
