'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 });
var cron = require('cron');

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

/* Daily cronjob to notify subscribers*/
var job = new cron.CronJob('00 10 * * 1-5', function () {
    console.log('Cron job started');
    notifySubscribers();
}, null, true,'Europe/Zurich');

/* Routes */
tgBot.onText(/\/get(Today)?$/mg,getTodayHandler);
tgBot.onText(/\/getDaily/,getDailyHandler);
tgBot.onText(/\/getPartTime/,getPartTimeHandler);
tgBot.onText(/\/start/,startHandler);
tgBot.onText(/\/stop/,cancelSubscriptionsHandler);
tgBot.onText(/\/cancel/,cancelSubscriptionsHandler);
tgBot.onText(/\/(get)?source/mg,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. You can send me /stop to cancel the daily update.";
        tgBot.sendMessage(chatId,markdownText,{ parse_mode: 'Markdown'});
    });
}

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

    let markdownText = "This feature is not implemented yet.";
    tgBot.sendMessage(chatId,markdownText,{ parse_mode: 'Markdown'});

}

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;

    var markdownText = 'Hello! \n' +
        'I can send you the menu for the SV restaurant in Zug. \n' +
        'try /get or /getDaily (for daily updates)';
    tgBot.sendMessage(chatId,markdownText,{ parse_mode: 'Markdown'});
}

function notifySubscribers() {
    var 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'});
}

@@ -180,16 +180,21 @@
 %7D);%0A
-var cron
+const CronJob
  = r
@@ -207,16 +207,24 @@
 ('cron')
+.CronJob
 ;%0A%0Aconst
@@ -356,27 +356,22 @@
 s*/%0A
-var job = new cron.
+try %7B%0A    new 
 Cron
@@ -401,24 +401,28 @@
 nction () %7B%0A
+    
     console.
@@ -450,16 +450,20 @@
 ');%0A    
+ 
+   
 notifySu
@@ -475,16 +475,20 @@
 bers();%0A
+    
 %7D, null,
@@ -510,16 +510,75 @@
 urich');
+%0A%7D catch(ex) %7B%0A    console.log(%22cron pattern not valid%22);%0A%7D
 %0A%0A/* Rou
@@ -1645,16 +1645,35 @@
 now on. 
+%5Cn%22 +%0A            %22
 You can 
@@ -1693,31 +1693,17 @@
  to 
-cancel the daily upd
+quit th
 at
-e
 .%22;%0A

check cron job on start