<html>
	<head>
		<meta charset="utf-8"> 
		<style type="text/css">
			textarea {
				width: 500px;
				height: 500px;
			}



		</style>
	</head>
	



	<body>
		<div id='container'>
			<form id='in'>
			</form>
		</div>
		<div id='log'>
			<textarea id='logs'>
				
			</textarea>
		</div>
		<script>

			function generateInput(parameters){
 			 	let container = document.getElementById('in');

 			 	let list = document.createElement("select");
 			 	list.id = "list_";
 			 	container.appendChild(list);
 			 	

	 			//let json = JSON.parse(parameters);
				parameters.map ( e => {
					let element = document.createElement("option");
					element.value = e;
					element.innerHTML = e;
					list.appendChild(element);
				})

				let arguments = document.createElement("input");
				arguments.id = "args";
				container.appendChild(arguments);

				let button = document.createElement("Button");
				button.innerHTML = "Execute";
				container.appendChild(button);
				button.onclick = request;

 			 	//container.appendChild(foo);
			}


			function request(){
			 	let app = document.querySelector('#list_');
				let val = app.options[app.selectedIndex].value;
			 	let parameter = document.getElementById('args').value;
			 	console.log(parameter);
				fetch("/apps/start/" + val + "/" + parameter).then(console.log);

			}


			//async function initLog(){
			//	socket = new WebSocket ( "/logs" );
	    	//	socket.onmessage = function ( evt ) { document.getElementById('logs').innerHTML += evt.data; };
			//}

			var sleep = time => new Promise(resolve => setTimeout(resolve, time))
			var poll = (promiseFn, time) => promiseFn().then(
            	sleep(time).then(() => poll(promiseFn, time)))
			poll(() => new Promise(() => fetch("/apps/log").then(res => res.text().then(e => { document.getElementById('logs').innerHTML = e;}))      ), 1000)

			let apps = fetch("/apps/list").then(res => res.json().then(generateInput));

			//generateInput("[ \"bla\", \"foo\", \"baz\" ]");

		</script>
	</body>



</html>