const express = require('express');
const favicon = require('serve-favicon');
const app = express();

const teamData = require( './modules/teamdata' );

app.set('port', (process.env.PORT || 5000));
app.use(express.static('static'));
app.use(favicon(__dirname + '/static/images/noun_55243_cc.png'));

const calendar = require('./modules/calendar');
const average = require('./modules/average');

app.get('/calendar', async (req, res) => {
    if(req.query.team && !teamData(req.query.team)) {
        res.sendStatus(404);

        return true;
    }
    const calendarString = await calendar(req.query.team || [] );

    if (req.query.team) {
        console.log(req.query.team);
    } else {
        console.log("no-filter");
    };

    if( req.query.download ){
        res.setHeader('Content-disposition', 'attachment; filename=hockey-mchockeyface.ics');
    }

    res.set('Content-Type', 'text/calendar');
    res.send(calendarString);
});

app.get('/average', async (req, res) => {
  res.set('Content-Type', 'text/plain');
  res.send(await average());
})

app.listen(app.get('port'), function () {
    console.log('Hockey McHockeyFace ready to serve on port', app.get('port'))
});

@@ -439,54 +439,551 @@
     
-if(req.query.team && !teamData(req.query.team)
+let requestedTeams = %5B%5D;%0A    if(req.query.team && Array.isArray(req.query.team))%7B%0A        requestedTeams = req.query.team;%0A    %7D else if (req.query.team)%7B%0A        requestedTeams = %5Breq.query.team%5D;%0A    %7D%0A%0A    requestedTeams = requestedTeams.filter(teamName =%3E %7B%0A        const hasTeamData = teamData(teamName);%0A%0A        if( !hasTeamData )%7B%0A            console.log(%60Stripping unknown team $%7BteamName%7D from requested teams%60);%0A%0A            return false;%0A        %7D%0A%0A        return true;%0A    %7D);%0A%0A    if(req.query.team && requestedTeams.length === 0
 ) %7B%0A
@@ -1039,16 +1039,17 @@
 ;%0A    %7D%0A
+%0A
     cons
@@ -1089,26 +1089,19 @@
 (req
-.query.team %7C%7C %5B%5D 
+uestedTeams
 );%0A%0A

handle multiple teams