const Extra = require('telegraf').Extra;

module.exports = {
	getCalendar: function () {
		return Extra.HTML().markup((m) => {
			let page = [];
			addDays(page, m);
			return m.inlineKeyboard(page);
		});
	},

	setDateListener: function (bot, onDateSelected) {
		bot.action(/calendar-telegram-\d+/g, context => {
			if (onDateSelected) {
				let date = context.match[0].replace("calendar-telegram-", "");
				onDateSelected(context, date);
			}
		});
	}
}

function addDays(page, m) {
	let date = new Date();
	let maxDays = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();

	let currentRow = new Array(7).fill(m.callbackButton(" ", "calendar-telegram-ignore"));
	for (var d = 1; d <= maxDays; d++) {
		date.setDate(d);

		let weekDay = date.getDay();
		//currentRow[weekDay] = toYyyymmdd(date);
		currentRow[weekDay] = m.callbackButton(d.toString(), "calendar-telegram-" + toYyyymmdd(date));

		if (weekDay == 6 || d == maxDays) {
			page.push(currentRow);
			currentRow = new Array(7).fill(m.callbackButton(" ", "calendar-telegram-ignore"));
		}
	}
}

function toYyyymmdd(date) {
	let mm = date.getMonth() + 1; // getMonth() is zero-based
	let dd = date.getDate();

	return [
		date.getFullYear(),
		(mm > 9 ? '' : '0') + mm,
		(dd > 9 ? '' : '0') + dd
	].join('');
}
@@ -35,16 +35,331 @@
 Extra;%0A%0A
+const weekDayNames = %5B%22S%22, %22M%22, %22T%22, %22W%22, %22T%22, %22F%22, %22S%22%5D;%0A// const monthNames = %5B%22January%22, %22February%22, %22March%22, %22April%22, %22May%22, %22June%22,%0A// %09%22July%22, %22August%22, %22September%22, %22October%22, %22November%22, %22December%22%5D;%0Aconst monthNames = %5B%22Jan%22, %22Feb%22, %22Mar%22, %22Apr%22, %22May%22, %22Jun%22,%0A%09%22Jul%22, %22Aug%22, %22Sep%22, %22Oct%22, %22Nov%22, %22Dec%22%5D;%0A%0A
 module.e
@@ -439,47 +439,8 @@
 %3E %7B%0A
-%09%09%09let page = %5B%5D;%0A%09%09%09addDays(page, m);%0A
 %09%09%09r
@@ -462,20 +462,38 @@
 eyboard(
-pa
 ge
+tPage(m, new Date())
 );%0A%09%09%7D);
@@ -739,18 +739,668 @@
 %7D%0A%09%09%7D);%0A
-%09%7D
+%0A%09%09bot.action(%22calendar-telegram-prev%22, context =%3E %7B%0A%09%09%09context.reply(%22prev%22);%0A%09%09%7D);%0A%0A%09%09bot.action(%22calendar-telegram-next%22, context =%3E %7B%0A%09%09%09context.reply(%22next%22);%0A%09%09%7D);%0A%09%7D%0A%7D%0A%0Afunction addHeader(page, m, date) %7B%0A%09let monthName = monthNames%5Bdate.getMonth()%5D;%0A%09let year = date.getFullYear();%0A%0A%09//page.push(%5Bm.callbackButton(monthName + %22 %22 + year, %22calendar-telegram-ignore%22)%5D);%0A%09page.push(%5B%0A%09%09m.callbackButton(%22%3C%22, %22calendar-telegram-prev%22),%0A%09%09m.callbackButton(monthName + %22 %22 + year, %22calendar-telegram-ignore%22),%0A%09%09m.callbackButton(%22%3E%22, %22calendar-telegram-next%22)%0A%09%5D);%0A%0A%09page.push(weekDayNames.map(e =%3E m.callbackButton(e, %22calendar-telegram-ignore%22)));
 %0A%7D%0A%0Afunc
@@ -1419,21 +1419,29 @@
 (page, m
+, date
 ) %7B%0A%09
+//
 let date
@@ -2010,24 +2010,137 @@
 ;%0A%09%09%7D%0A%09%7D%0A%7D%0A%0A
+function getPage(m, date) %7B%0A%09let page = %5B%5D;%0A%09addHeader(page, m, date);%0A%09addDays(page, m, date);%0A%09return page;%0A%7D%0A%0A
 function toY

test calendar: header handling