'use strict';
const TelegramBot = require('node-telegram-bot-api');
const bot_api_token = process.env.BOT_API_TOKEN;
const tgBot  = new TelegramBot(bot_api_token, { polling: true });
const CronJob = require('cron').CronJob;

const Parser = require('./SVPageParser');
const Subscriptions = require('./subscriptions');

/* Daily cronjob to notify subscribers*/
try {
    new CronJob('00 10 * * 1-5', function () {
        console.log('Cron job started');
        notifySubscribers();
    }, null, true,'Europe/Zurich');
} catch(ex) {
    console.log("cron pattern not valid");
}

/* Routes */
tgBot.onText(/\/get(Today)?$/mgi,getTodayHandler);
tgBot.onText(/\/(get)?Daily/i,getDailyHandler);
tgBot.onText(/\/(get)?PartTime/i,getPartTimeHandler);
tgBot.onText(/\/start/i,startHandler);
tgBot.onText(/\/stop/i,cancelSubscriptionsHandler);
tgBot.onText(/\/cancel/i,cancelSubscriptionsHandler);
tgBot.onText(/\/(get)?source/mgi,getSourceHandler);

/* Handlers */
function sendTodaysMenu(chatId) {
    const url = 'http://siemens.sv-restaurant.ch/de/menuplan.html';
    const parser = new Parser();

    parser.parse(url, function (markdownText) {
        tgBot.sendMessage(chatId, markdownText, {parse_mode: 'Markdown'});
    });
}
function getTodayHandler(message) {
    let chatId = message.chat.id;
    sendTodaysMenu(chatId);
}

function getDailyHandler(message) {
    let chat = message.chat;
    let chatId = message.chat.id;

    const subscriptions = new Subscriptions();
    subscriptions.add(chat,function () {
        let markdownText = "*Successfully added you to the daily subscriber list* \n" +
            "I will send you the menu at 10:00am from now on. \n" +
            "You can send me /stop to quit that.";
        tgBot.sendMessage(chatId,markdownText,{ parse_mode: 'Markdown'});
    });
}

function getPartTimeHandler(message) {
    let chatId = message.chat.id;

    let markdownText = "Please select all weekdays you want to be notified.";

    let options = {
        "parse_mode": "Markdown",
        "reply_markup": {
                "keyboard": [
                    [{text: "Monday"}],
                    [{text: "Tuesday"}],
                    [{text: "Wednesday"}],
                    [{text: "Thursday"}],
                    [{text: "Friday"}],
                    [{text: "Done! Send me the menu on these days."}]
                ],
        "selective": true
        }
    };

    tgBot.sendMessage(chatId, markdownText, options);

}

function cancelSubscriptionsHandler(message) {
    let chat = message.chat;
    let chatId = message.chat.id;

    const subscriptions = new Subscriptions();
    subscriptions.remove(chat,function () {
        let markdownText = "*Successfully removed you from the daily subscriber list* \n" +
            "I will no longer bother you with daily updates.";
        tgBot.sendMessage(chatId,markdownText,{ parse_mode: 'Markdown'});
    });
}

function startHandler(message) {
    let chatId = message.chat.id;

    let markdownText = 'Hello! \n' +
        'I can send you the menu for the SV restaurant in Zug. \n' +
        'try /get or /Daily (for daily updates)';

        tgBot.sendMessage(chatId,markdownText, { parse_mode: 'Markdown'});
}

function notifySubscribers() {
    let subscriptions = new Subscriptions();

    console.log("notify Subscribers called");
    subscriptions.forAll(function (subscriber) {
            sendTodaysMenu(subscriber.id);
    });
}

function getSourceHandler(message){
    let chatId = message.chat.id;

    let markdownText = 'This bot is written by Fabio Zuber utilizing Node.js.\n' +
        'The code is open source. Feel free to check it out on [GitHub](https://github.com/Sirius-A/sv-restaurant-zug-bot).';
    tgBot.sendMessage(chatId,markdownText,{ parse_mode: 'Markdown'});
}

@@ -311,16 +311,83 @@
 ons');%0A%0A
+const weekdayRegex = /Monday%7CTuesday%7CWednesday%7CThursday%7CFriday/i;%0A%0A
 /* Daily
@@ -1000,16 +1000,59 @@
 andler);
+%0AtgBot.onText(weekdayRegex,weekdayHandler);
 %0A%0A/* Han
@@ -2511,17 +2511,16 @@
 %0A    %7D;%0A
-%0A
     tgBo
@@ -2565,16 +2565,52 @@
 tions);%0A
+%7D%0A%0Afunction weekdayHandler() %7B%0A%0A    
 %0A%7D%0A%0Afunc

implement handler for weekdays