/*
 * 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_manifestation
			{
				
				export namespace mod_svg
					{
						
						export namespace mod_token
							{
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								export type type_token =
									{
										model : mod_vtm.mod_model.mod_token.type_token;
									}
								;
								
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								function create
									(
										model : mod_vtm.mod_model.mod_token.type_token
									)
									: type_token
									{
										return {
											"model": model,
										};
									}
								
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								export function create_extended
									(
										model : mod_vtm.mod_model.mod_token.type_token
									)
									: type_manifestation<mod_vtm.mod_model.mod_token.type_token>
									{
										return {
											"kind": "svg_token",
											"data": create(model),
										};
									}
								
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								function view
									(
										token_ : type_token
									)
									: lib_xml.type_node
									{
										let token : mod_vtm.mod_model.mod_token.type_token = token_.model;
										let children_token : Array<lib_xml.type_node> = [];
										// Stein
										{
											let node_stone : lib_xml.type_node = (
												lib_xml.create_normal
													(
														"circle",
														{
															"cx": (0.0).toFixed(lib_svg.float_precision),
															"cy": (0.0).toFixed(lib_svg.float_precision),
															"r": (0.125).toFixed(lib_svg.float_precision),
															"class": "stone",
														}
													)
											);
											children_token.push(node_stone);
										}
										// Band
										{
											let tape : Array<mod_vtm.mod_model.mod_symbol.type_symbol> = mod_vtm.mod_model.mod_token.tape_read(token);
											let children_tape : Array<lib_xml.type_node> = [];
											tape.forEach
												(
													(symbol, index) =>
														{
															let r : float = 0.06125;
															let x : float = (+0.1+(2*r*1.25)*index);
															let y : float = (-0.1);
															let node_entry : lib_xml.type_node = (
																lib_xml.create_normal
																	(
																		"circle",
																		{
																			"cx": x.toFixed(lib_svg.float_precision),
																			"cy": y.toFixed(lib_svg.float_precision),
																			"r": r.toFixed(lib_svg.float_precision),
																			/*
																			"x": (x-r).toFixed(lib_svg.float_precision),
																			"y": (y-r).toFixed(lib_svg.float_precision),
																			"width": (2*r).toFixed(lib_svg.float_precision),
																			"height": (2*r).toFixed(lib_svg.float_precision),
																			 */
																			"class": (
																				[
																					"entry",
																					"symbol_" + symbol.toFixed(0),
																				].join(" ")
																			),
																		}
																	)
															);
															children_tape.push(node_entry);
														}
												)
											;
											let node_tape = (
												lib_xml.create_normal
													(
														"g",
														{
															"class": "tape",
														},
														children_tape
													)
											);
											children_token.push(node_tape);
										}
										let position : mod_position.type_position = mod_position.from_spot(mod_vtm.mod_model.mod_token.spot_read(token));
										let node_token = (
											lib_xml.create_normal
												(
													"g",
													{
														"class": "token",
														"transform": lib_svg.translation(position.x, position.y),
													},
													children_token
												)
										);
										return node_token;
									}
								
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								function bind(token : type_token) : void
									{
									}
								
								
								/**
								 * @author kcf <vidofnir@folksprak.org>
								 */
								lib_trait.attend<signature_manifestation<mod_vtm.mod_model.mod_token.type_token, lib_xml.type_node>>
									(
										trait_manifestation,
										"svg_token",
										{
											"view": (manifestation) => view(manifestation.data),
											"bind": (manifestation) => bind(manifestation.data),
										}
									)
								;
								
							}
						
					}
				
			}
		
	}

