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

module.exports = {
	getCalendar: function () {
		return Extra.HTML().markup((m) => {
			let page = [];
			addDays(page, m);
			console.log(page);
			// m.inlineKeyboard([
			// 	[m.callbackButton("1", "calendar-telegram-1"), m.callbackButton("2", "calendar-telegram-2")],
			// 	[m.callbackButton("3", "calendar-telegram-3"), m.callbackButton("4", "calendar-telegram-4")]
			// ]);
			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('');
}
@@ -163,266 +163,8 @@
 m);%0A
-%09%09%09console.log(page);%0A%09%09%09// m.inlineKeyboard(%5B%0A%09%09%09// %09%5Bm.callbackButton(%221%22, %22calendar-telegram-1%22), m.callbackButton(%222%22, %22calendar-telegram-2%22)%5D,%0A%09%09%09// %09%5Bm.callbackButton(%223%22, %22calendar-telegram-3%22), m.callbackButton(%224%22, %22calendar-telegram-4%22)%5D%0A%09%09%09// %5D);%0A
 %09%09%09r
@@ -193,16 +193,16 @@
 (page);%0A
+
 %09%09%7D);%0A%09%7D
@@ -629,32 +629,33 @@
 callbackButton(%22
+ 
 %22, %22calendar-tel
@@ -966,16 +966,16 @@
 ntRow);%0A
-
 %09%09%09curre
@@ -1018,16 +1018,17 @@
 Button(%22
+ 
 %22, %22cale

test calendar