'use strict';

const _ = require('lodash');

module.exports = function Template(options) {
	const o = Object.assign({}, {
		ignoreEventTypes: [],
		title: 'Unknown event',
		version: new Date().toString(),
		acronym: options.title || '?',
	}, options);

	// Level one
	return function template({ eventsByLanguage, eventsByTime }) {
		const { day } = eventsByTime[0];
		const events = _(eventsByTime).map(eventTemplate).join('\n');
		const header = `<strong>Translations for ${o.acronym}</strong> ·
			<strong>Day ${day}</strong><br>
			Version ${o.version}<br><br>`;
		const tableOfContents = `<strong>Overview by language</strong><br><br>` + _(eventsByLanguage).map(eventsByLanguageTemplate).join('<br>');
		return header + events + tableOfContents;
	}

	// Level two
	// Generates the heading for language groups and hands the events
	// down to the next level
	function eventsByLanguageTemplate({ events, language }) {
		const title = ({
			de: 'de → en',
			en: 'en → de',
		})[language] || language;

		return `
			<strong>${title}</strong><br>
			${events.map(tocTemplate).join('\n')}
			`;
	}

	// Level three a
	// Generates the listing for each individual event
	function eventTemplate(event) {
		const { title, room, start, duration, id, language, sequentialNumber } = event;
		const speakers = event.persons.map(p => p.name).join(', ');
		const type = o.ignoreEventTypes.includes(event.type) ? '' : ` (${_.upperFirst(event.type)})`;

		const targetLanguages = ({
			en: '&nbsp;&nbsp;&nbsp;&nbsp;→ de ',
			de: '&nbsp;&nbsp;&nbsp;&nbsp;→ en ',
		})[language] || '&nbsp;&nbsp;&nbsp;&nbsp;→ ';

		// Yes, this is awful HTML, but its only job is to be parsed by EtherPad,
		// so I won't even bother cleaning up the indentation.
		return `
			#${sequentialNumber}<br>
			[${language}] <strong>${start}</strong> +${duration}, ${room}<br>
			<strong>${title}</strong>${type}<br>
			${speakers}<br>
			https://fahrplan.events.ccc.de/congress/2016/Fahrplan/events/${id}.html<br>
			${targetLanguages}<br>
			<br>
			<br>
			<br>
			`;
	}

	// Level three b
	// Generates the listing for the table of contents
	function tocTemplate(event) {
		const { start, duration, title, sequentialNumber } = event;
		return `<strong>${start}</strong>
		+${duration}
		#${sequentialNumber}
		${title}
		<br>`
	}
}

@@ -1259,32 +1259,38 @@
 sequentialNumber
+, guid
  %7D = event;%0A%09%09co
@@ -1285,16 +1285,16 @@
  event;%0A
-
 %09%09const 
@@ -1485,72 +1485,24 @@
 n: '
-&nbsp;&nbsp;&nbsp;&nbsp;%E2%86%92 de ',%0A%09%09%09de: '&nbsp;&nbsp;&nbsp;&nbsp;
+%E2%86%92 de ',%0A%09%09%09de: '
 %E2%86%92 en
@@ -1528,32 +1528,8 @@
 %7C%7C '
-&nbsp;&nbsp;&nbsp;&nbsp;
 %E2%86%92 ';
@@ -1833,16 +1833,26 @@
 %3Cbr%3E%0A%09%09%09
+Fahrplan: 
 https://
@@ -1915,24 +1915,98 @@
 d%7D.html%3Cbr%3E%0A
+%09%09%09Slides (if available): https://speakers.c3lingo.org/talks/$%7Bguid%7D/%3Cbr%3E%0A
 %09%09%09$%7BtargetL

Add link to file drop to templates