var express       = require('express');
var MatomoTracker = require('matomo-tracker');
var router        = express.Router();

/* GET home page */
router.get('/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  db.getItemList((items)=>{
    res.render('index', { title: 'Dein.Equipment', items: items })
  })
});

/* GET home page by sticker, v1 */
router.get('/by/sticker/v1/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  db.getItemList((items)=>{
    res.render('index', { title: 'Dein.Equipment', items: items })
  })
});

/* GET home page via instagram */
router.get('/via/instagram/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  db.getItemList((items)=>{
    res.render('index', { title: 'Dein.Equipment', items: items })
  })
});

/* GET home page via facebook */
router.get('/via/facebook/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  db.getItemList((items)=>{
    res.render('index', { title: 'Dein.Equipment', items: items })
  })
});

/* GET home page via direct link, given to others by human */
router.get('/via/directlink/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  db.getItemList((items)=>{
    res.render('index', { title: 'Dein.Equipment', items: items })
  })
});

router.get('/article/show/:identifier', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')

  const identifier = req.params.identifier

  db.getItem(identifier, (item)=>{
    db.getNotAvailableList(item.id, (notAvailableList)=>{
      item.unavailableDates = notAvailableList
      res.render('detail', { title: item.title, item: item })
    })  
  })
});

/* GET imprint */
router.get('/imprint/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  res.render('imprint', { title: 'Dein.Equipment Impressum' })
});

/* GET privacy */
router.get('/privacy/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  res.render('privacy', { title: 'Dein.Equipment Datenschutz' })
});

/* GET terms and conditions */
router.get('/terms/', function(req, res, next) {
  var app = req.app
  var db = app.get('DB')
  
  res.render('terms', { title: 'Dein.Equipment AGB' })
});

router.post('/request/', function(req, res, next) {
  var app = req.app
  var mailer = app.get('MAILER')
  
  const params = {
    id: req.body.itemID,
    title: req.body.itemTitle,
    start: req.body.start,
    end: req.body.end,
    name: req.body.name,
    contact: req.body.contact
  }
  
  mailer.mail(params)
  
  var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
  trackAtMatomo(app, fullUrl, 'Anfrage', params)
  
  res.render('request', { title: 'Dein.Equipment: Anfrage gesendet' })
})

function trackAtMatomo(app, url, actionName, data) {
  var matomo = new MatomoTracker(app.get('CONF').matomo.site.id, app.get('CONF').matomo.url)

  matomo.on('error', function(err) {
    app.get('LOG').error('Router | Error tracking request: ' + err);
  })

  matomo.track({
    url: url,
    action_name: actionName,        // Action, shown on dashboard
    ua: 'Dein.Equipment Backend',   // UserAgend
    data: JSON.stringify(data)
  })
}

module.exports = router;
