/*
 * 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
	{
		
		export namespace mod_model
			{
				
				export namespace mod_task
					{
						
						/**
						 * @author kcf <vidofnir@folksprak.org>
						 */
						export type type_task = lib_call.type_complex<Object>;
						
						
						/**
						 * @author kcf <vidofnir@folksprak.org>
						 */
						var _liste : Array<type_task> = [];
						
						
						/**
						 * @author kcf <vidofnir@folksprak.org>
						 */
						export function id
							(
								task : type_task
							)
							: string
							{
								return (
									lib_call.distinguish<string>
										(
											task,
											{
												"acceptortask": (data) => mod_acceptortask.id(data),
												"transducertask": (data) => mod_transducertask.id(data),
											}
										)
								);
							}
						
						
						/**
						 * @author kcf <vidofnir@folksprak.org>
						 */
						export function title
							(
								task : type_task
							)
							: string
							{
								return (
									lib_call.distinguish<string>
										(
											task,
											{
												"acceptortask": (data) => mod_acceptortask.title(data),
												"transducertask": (data) => mod_transducertask.title(data),
											}
										)
								);
							}
						
						
						/**
						 * @author kcf <vidofnir@folksprak.org>
						 */
						export function text
							(
								task : type_task
							)
							: string
							{
								return (
									lib_call.distinguish<string>
										(
											task,
											{
												"acceptortask": (data) => mod_acceptortask.text(data),
												"transducertask": (data) => mod_transducertask.text(data),
											}
										)
								);
							}
						
						
						/**
						 * @author kcf <vidofnir@folksprak.org>
						 */
						export function tests
							(
								task : type_task
							)
							: Array<mod_test.type_test>
							{
								return (
									lib_call.distinguish<Array<mod_test.type_test>>
										(
											task,
											{
												"acceptortask": (data) => (
													mod_acceptortask.tests(data)
													.map(data_ => ({"kind": "acceptortest", "data": data_}))
												),
												"transducertask": (data) => (
													mod_transducertask.tests(data)
													.map(data_ => ({"kind": "transducertest", "data": data_}))
												),
											}
										)
								);
							}
						
						
						/**
						 * @author kcf <vidofnir@folksprak.org>
						 */
						export function import_(task_raw : any) : type_task
							{
								switch (task_raw["kind"])
									{
										case "acceptor":
											{
												return {
													"kind": "acceptortask",
													"data": (
														mod_acceptortask.create
															(
																task_raw["id"],
																lib_translate.get(["tasks", task_raw["id"], "title"].join(".")),
																lib_translate.get(["tasks", task_raw["id"], "text"].join(".")),
																task_raw["parameter"]["tests"].map
																	(
																		test_raw => (
																			mod_acceptortest.create
																				(
																					test_raw["input"],
																					test_raw["accept"]
																				)
																		)
																	)
															)
													)
												};
												break;
											}
										case "transducer":
											{
												return {
													"kind": "transducertask",
													"data": (
														mod_transducertask.create
															(
																task_raw["id"],
																lib_translate.get(["tasks", task_raw["id"], "title"].join(".")),
																lib_translate.get(["tasks", task_raw["id"], "text"].join(".")),
																task_raw["parameter"]["tests"].map
																	(
																		test_raw => (
																			mod_transducertest.create
																				(
																					test_raw["input"],
																					test_raw["output"]
																				)
																		)
																	)
															)
													)
												};
												break;
											}
										default:
											{
												const message : string = "unbehandelte Art '" + task_raw["kind"] + "'";
												throw (new Error(message));
												break;
											}
									}
							}
						
						
						/**
						 * @author kcf <vidofnir@folksprak.org>
						 */
						export function registrieren(task : type_task) : void
							{
								_liste.push(task);
							}
						
						
						/**
						 * @author kcf <vidofnir@folksprak.org>
						 */
						export function get(index : int) : type_task
							{
								return _liste[index];
							}
						
					}
				
			}
		
	}

