/*
 * 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 lib_xml
	{
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export type type_node = lib_call.type_complex<any>;
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function create_text
			(
				content : string
			)
			: type_node
			{
				return (
					lib_call.wrap
						(
							"text",
							{
								"content": content
							}
						)
				);
			}
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function create_normal
			(
				name : string,
				attributes : {[key : string] : string} = {},
				children : Array<type_node> = []
			)
			: type_node
			{
				return (
					lib_call.wrap
						(
							"normal",
							{
								"name": name,
								"attributes": attributes,
								"children": children
							}
						)
				);
			}
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function view
			(
				node : type_node,
				depth : int = 0
			)
			: string
			{
				return (
					lib_call.distinguish<any>
						(
							node,
							{
								"text": ({"content": content}) =>
									{
										return content;
									}
								,
								"normal": ({"name": name, "attributes": attributes, "children": children}) =>
									{
										let str : string = "";
										// begin
										{
											let str_begin : string = "";
											str_begin += name;
											// attributes
											{
												let str_attributes : string = "";
												Object.keys(attributes).forEach
													(
														key =>
															{
																let value : string = attributes[key];
																str_attributes += (" " + key + "=" + ("\"" +  value + "\""));
															}
													)
												;
												str_begin += str_attributes;
											}
											str_begin = (lib_string.indent(depth) + "<" + str_begin + ">" + "\n");
											str += str_begin;
										}
										// children
										{
											children.forEach(child => (str += view(child, depth+1)));
										}
										// end
										{
											let str_end : string = "";
											str_end += name;
											str_end = (lib_string.indent(depth) + "<" + "/" + str_end + ">" + "\n");
											str += str_end;
										}
										return str;
									}
								,
							}
						)
				);
			}
		
	}

