/*
 * Verrückte Turing-Maschinen — A turing complete game
 * Copyright (C) 2016-2018 kcf <vidofnir@folksprak.org>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

namespace mod_vtm
	{
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		function tasks_insert(handler : (task : mod_model.mod_task.type_task)=>void) : void
			{
				const tasks_raw_ : lib_errormonade.type_errormonade<Array<any>> = mod_vtm.mod_data.read("tasks");
				if (lib_errormonade.filled(tasks_raw_))
					{
						let tasks_raw : Array<any> = lib_errormonade.read(tasks_raw_);
						let dom_selection : Element = document.querySelector("#task_selection");
						tasks_raw.forEach
							(
								(task_raw, index) =>
									{
										let task : mod_model.mod_task.type_task = mod_model.mod_task.import_(task_raw);
										// Aufgabe registrieren
										{
											mod_model.mod_task.registrieren(task);
										}
										// Option entryen
										{
											let prefix : string = (
												{
													"acceptor": lib_translate.get("model.tasks.kinds.acceptor.shortcut"),
													"transducer": lib_translate.get("model.tasks.kinds.transducer.shortcut"),
												}[task_raw["kind"]]
											);
											let title : string = ("[" + prefix + "]" + " " + mod_model.mod_task.title(task));
											let value : string = index.toFixed(0);
											let dom_option : Element = document.createElement("option");
											dom_option.setAttribute("value", value);
											dom_option.textContent = title;
											dom_selection.appendChild(dom_option);
										}
									}
							)
						;
						dom_selection.addEventListener
							(
								"change",
								event =>
									{
										let value : string = dom_selection["value"];
										let index : int = parseInt(value);
										let task : mod_model.mod_task.type_task = mod_model.mod_task.get(index);
										handler(task);
									}
							)
						;
					}
				else
					{
						console.warn("Daten nicht auffindbar");
					}
			}


		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		function main() : void
			{
				// Übersetzungen
				{
					const languages : Array<string> = lib_list.copy<string>(navigator.languages);
					const language_native : string = "de";
					// if (! languages.includes(language_native))
					if (! languages.some((language) => (language === language_native)))
						languages.push(language_native);
					lib_translate.setup(languages);
					lib_translate.deploy(document);
				}
				// Hilfe
				{
					// Einleitung
					{
						document.querySelector("#help_introduction").innerHTML = (
							lib_translate.get
								(
									"help.introduction",
									{
										"manufacturia": "<a href=\"http://www.kongregate.com/games/PleasingFungus/manufactoria\">Manufacturia</a>",
										"crazy_machines": "<a href=\"http://www.crazy-machines.com/\">Crazy Machines</a>",
										"world_of_goo": "<a href=\"http://worldofgoo.com/\">World of Goo</a>",
									}
								)
						);
					}
					// Aktoren
					{
						// Arten
						{
							const from_x : float = -0.5;
							const from_y : float = -0.5;
							const to_x : float = +0.5;
							const to_y : float = +0.5;
							const width : float = 80;
							const height : float = 80;
							[
								{
									"model": mod_model.mod_actuator.example("generator"),
									"area": document.querySelector("#help_actuators_actuator_generator"),
								},
								{
									"model": mod_model.mod_actuator.example("acceptor"),
									"area": document.querySelector("#help_actuators_actuator_acceptor"),
								},
								{
									"model": mod_model.mod_actuator.example("rejector"),
									"area": document.querySelector("#help_actuators_actuator_rejector"),
								},
								{
									"model": mod_model.mod_actuator.example("conveyer"),
									"area": document.querySelector("#help_actuators_actuator_conveyer"),
								},
								{
									"model": mod_model.mod_actuator.example("writer"),
									"area": document.querySelector("#help_actuators_actuator_writer"),
								},
								{
									"model": mod_model.mod_actuator.example("reader"),
									"area": document.querySelector("#help_actuators_actuator_reader"),
								},
							]
							.forEach
								(
									entry =>
										{
											const manifestation = (
												mod_manifestation.mod_svg.mod_actuator.create_extended
													(
														entry.model,
														mod_model.mod_spot.null_()
													)
											);
											const xmlnode : lib_xml.type_node = (
												lib_svg.root
													(
														from_x, from_y,
														to_x, to_y,
														width, height,
														[mod_manifestation.view(manifestation)]
													)
											);
											entry.area.querySelector(".help_actuators_actuator_image").innerHTML = lib_xml.view(xmlnode);
										}
								)
							;
						}
					}
				}
				// Spiel
				{
					let game : mod_model.mod_game.type_game;
					// Aufgaben
					{
						tasks_insert
							(
								function (task : mod_model.mod_task.type_task) : void {mod_model.mod_game.task_set(game, task);}
							)
						;
					}
					// Aufbau
					{
						game = mod_model.mod_game.create(mod_model.mod_task.get(0));
					}
					// Manifestationen
					{
						[
							mod_manifestation.mod_web.mod_game.create_extended
								(
									game,
									document.querySelector("#section_mid")
								)
							,
							mod_manifestation.mod_store.mod_game.create_extended
								(
									game
								)
							,
						]
						.forEach(mod_manifestation.setup);
					}
				}
			}


		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function entry_web() : void
			{
				document.addEventListener
					(
						"DOMContentLoaded",
						event =>
							{
								// music
								{
									let dom_audio : HTMLAudioElement = document.querySelector<HTMLAudioElement>("#music");
									
									const playlist : Array<string> =
									[
										"time",
										"marble_machine",
										"elan",
										"lux_aeterna",
										"sweet_dreams",
									];
									let index : int = -1;
									const next = function ()
										{
											index = ((index + 1) % playlist.length);
											const path : string = ("music/" + playlist[index] + ".ogg");
											let dom_source : HTMLSourceElement = document.createElement("source");
											dom_source.setAttribute("type", "audio/ogg");
											dom_source.setAttribute("src", path);
											for (let i : int = 0; i < dom_audio.children.length; i += 1) dom_audio.removeChild(dom_audio.children[i]);
											dom_audio.appendChild(dom_source);
											dom_audio.load();
											dom_audio.play();
										}
									;
									dom_audio.volume = 0.25;
									// dom_audio.setAttribute("autoplay", "autoplay");
									// dom_audio.setAttribute("loop", "loop");
									// dom_audio.setAttribute("controls", "controls");
									dom_audio.addEventListener("ended", () => {next();});
									next();
								}
								main();
							}
					)
				;
			}
		
	}

