'use strict'

const html = require('pithy')
const beautify = require('js-beautify').html
const options = require('../api').options

const head = () => {
	const elements = [
		html.meta({charset: 'utf-8'}),
		html.meta({name: 'viewport', content: "width=device-width, initial-scale=1.0"}),
		html.title(null, 'Bahn-Preiskalender'),
		html.link({rel: 'stylesheet', type: 'text/css', href: 'assets/reset.css'}),
		html.link({rel: 'stylesheet', type: 'text/css', href: 'assets/base.css'}),
		html.link({rel: 'stylesheet', type: 'text/css', href: 'assets/main.css'}),
		html.link({rel: 'stylesheet', type: 'text/css', href: 'assets/autocomplete.css'}),
		html.link({rel: 'stylesheet', type: 'text/css', href: 'assets/'+require('config').api+'.css'})
	]
	return html.head(null, elements)
}

const errorBox = (params) => {
	if(params.error) return html.div({id: 'error', class: 'subtitle'}, [html.span(null, params.error)])
	return []
}

const generate = (params) => {
	let document = '<!doctype html>' + html.html(null, [
		head(),
		html.body(null, [
			html.form({id: 'page', action: '/calendar', method: 'GET'}, [
				html.div('#header', [html.h1(null, 'Preiskalender')]),
				errorBox(params),
				html.div('#form', [
					html.div({id: 'origin', class: 'station'}, [html.span(null, 'Ab'), html.input({id: 'from', name: 'from', type: 'text', value: (params.from) ? params.from.name : '', placeholder: 'Startbahnhof', size: 1})]),
					html.div({id: 'destination', class: 'station'}, [html.span(null, 'An'), html.input({id: 'to', name: 'to', type: 'text', value: (params.to) ? params.to.name : '', placeholder: 'Zielbahnhof', size: 1})]),
					html.div('#go', [html.input({id: 'submit', name: 'submit', type: 'submit', value: 'Suchen'})])
				]),
				html.div('#options', options.input(params))
			]),
			html.div('#footer', [
				html.a({id: 'faq', href: '/faq'}, 'FAQ'),
				html.span(null, ' – '),
				html.a({id: 'impressum', href: '/impressum'}, 'Rechtliches')
			]),
			html.script({src: 'assets/bundle.js'})
		])
	])
	return beautify(document)
}

module.exports = generate

@@ -125,16 +125,332 @@
 ptions%0A%0A
+const opengraph = %5B%0A%09%09html.meta(%7Bname: 'og:title', value: 'bahn.guru - der Bahn-Preiskalender'%7D),%0A%09%09html.meta(%7Bname: 'og:description', value: 'Der Bahn-Guru hilft dir dabei, die g%C3%BCnstigsten Sparpreise der Deutschen Bahn zu finden. %F0%9F%9A%85'%7D),%0A%09%09html.meta(%7Bname: 'og:image', value: 'https://i.imgur.com/bJmvAJp.png'%7D),%0A%5D%0A%0A%0A
 const he
@@ -640,16 +640,32 @@
 nder'),%0A
+%09%09...opengraph,%0A
 %09%09html.l

add basic opengraph tags