/*
 * 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_svg
	{
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export const float_precision : int = 4;
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function rotation
			(
				staerke : float
			)
			: string
			{
				return (
					"rotate"
					+ "("
					+ (staerke * 360).toFixed(float_precision)
					+ ")"
				);
			}
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function translation
			(
				x : float,
				y : float
			)
			: string
			{
				return (
					"translate"
					+ "("
					+ x.toFixed(float_precision)
					+ ", "
					+ y.toFixed(float_precision)
					+ ")"
				);
			}
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function scaling
			(
				staerke : float
			)
			: string
			{
				return (
					"scale"
					+ "("
					+ staerke.toFixed(float_precision)
					+ ")"
				);
			}
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function path_description
			(
				vertices : Array<lib_vector.type_vector>,
				close : boolean = true
			)
			: string
			{
				let d : string = "";
				vertices.forEach
					(
						(vertex, index) =>
							{
								let c : string = ((index <= 0) ? "M" : "L");
								let x : string = vertex.x.toFixed(float_precision);
								let y : string = vertex.y.toFixed(float_precision);
								d += [c, x, y].join(" ");
							}
					)
				;
				if (close)
					d += "Z";
				return d;
			}
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function path
			(
				vertices : Array<lib_vector.type_vector>,
				close : boolean = true,
				attributes : {[name : string] : string} = {},
			)
			: lib_xml.type_node
			{
				attributes["d"] = path_description(vertices, close);
				return lib_xml.create_normal("path", attributes);
			}
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function group
			(
				attributes : {[key : string] : string} = {},
				children : Array<lib_xml.type_node> = []
			)
			: lib_xml.type_node
			{
				return (
					lib_xml.create_normal
						(
							"g",
							attributes,
							children
						)
				);
			}
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function circle
			(
				center_x : float,
				center_y : float,
				radius : float,
				classes : Array<string> = []
			)
			: lib_xml.type_node
			{
				return (
					lib_xml.create_normal
						(
							"circle",
							{
								"cx": center_x.toFixed(float_precision),
								"cy": center_y.toFixed(float_precision),
								"r": radius.toFixed(float_precision),
								"class": classes.join(" "),
							}
						)
				);
			}
		
		
		/**
		 * @author kcf <vidofnir@folksprak.org>
		 */
		export function root
			(
				from_x : float,
				from_y : float,
				to_x : float,
				to_y : float,
				height : int = 500,
				width : int = 500,
				children : Array<lib_xml.type_node> = []
			)
			: lib_xml.type_node
			{
				return (
					lib_xml.create_normal
						(
							"svg",
							{
								"xmlns": "http://www.w3.org/2000/svg",
								"xmlns:xlink": "http://www.w3.org/1999/xlink",
								"width": width.toFixed(0),
								"height": height.toFixed(0),
								"viewBox": [from_x.toFixed(4), from_y.toFixed(4), (to_x-from_x).toFixed(4), (to_y-from_y).toFixed(4)].join(" "),
							},
							children,
						)
				);
			}

	}

