// Generated by Haxe 4.3.3
package schema;

import haxe.root.*;

@SuppressWarnings(value={"rawtypes", "unchecked"})
public class MLDToSVG extends haxe.lang.HxObject
{
	public MLDToSVG(haxe.lang.EmptyObject empty)
	{
	}
	
	
	public MLDToSVG()
	{
		//line 6 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		schema.MLDToSVG.__hx_ctor_schema_MLDToSVG(this);
	}
	
	
	protected static void __hx_ctor_schema_MLDToSVG(schema.MLDToSVG __hx_this)
	{
	}
	
	
	public static haxe.root.Xml generateSVG(model.MLD mld, boolean typed)
	{
		//line 13 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml doc = haxe.root.Xml.createDocument();
		//line 14 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml svg = schema.Svg.initSvg(doc);
		//line 16 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		{
			//line 16 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			java.lang.Object relation = new haxe.ds._StringMap.StringMapValueIterator<model.Relation>(((haxe.ds.StringMap<model.Relation>) (mld.relations) ));
			//line 16 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			while (haxe.lang.Runtime.toBool(((java.lang.Boolean) (haxe.lang.Runtime.callField(relation, "hasNext", null)) )))
			{
				//line 16 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				model.Relation relation1 = ((model.Relation) (haxe.lang.Runtime.callField(relation, "next", null)) );
				//line 17 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				relation1.svg = schema.MLDToSVG.generateTableSvg(relation1, typed);
				//line 18 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				svg.addChild(relation1.svg);
			}
			
		}
		
		//line 21 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		schema.Svg.placeObjects(mld.relations, svg, mld.height, mld.width);
		//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		{
			//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			java.lang.Object relation2 = new haxe.ds._StringMap.StringMapValueIterator<model.Relation>(((haxe.ds.StringMap<model.Relation>) (mld.relations) ));
			//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			while (haxe.lang.Runtime.toBool(((java.lang.Boolean) (haxe.lang.Runtime.callField(relation2, "hasNext", null)) )))
			{
				//line 22 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				model.Relation relation3 = ((model.Relation) (haxe.lang.Runtime.callField(relation2, "next", null)) );
				//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				{
					//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
					int _g = 0;
					//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
					haxe.root.Array<java.lang.Object> _g1 = relation3.foreignKeys;
					//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
					while (( _g < _g1.length ))
					{
						//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
						java.lang.Object fk = _g1.__get(_g);
						//line 23 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
						 ++ _g;
						//line 24 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
						svg.addChild(schema.MLDToSVG.generateLineSVG(relation3, fk));
					}
					
				}
				
			}
			
		}
		
		//line 27 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		return doc;
	}
	
	
	public static haxe.root.Xml generateTableSvg(model.Relation relation, boolean typed)
	{
		//line 31 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		int chrWidth = relation.name.length();
		//line 32 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		{
			//line 32 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			int _g = 0;
			//line 32 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			haxe.root.Array<model.Field> _g1 = relation.fields;
			//line 32 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			while (( _g < _g1.length ))
			{
				//line 32 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				model.Field field = _g1.__get(_g);
				//line 32 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				 ++ _g;
				//line 33 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				int lineWidth = ( field.name.length() + (( (typed) ? (( field.dbType.length() + 2 )) : (0) )) );
				//line 34 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				if (( lineWidth > chrWidth )) 
				{
					//line 35 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
					chrWidth = lineWidth;
				}
				
			}
			
		}
		
		//line 38 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml g = haxe.root.Xml.createElement("g");
		//line 39 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml rect = haxe.root.Xml.createElement("rect");
		//line 40 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		g.addChild(rect);
		//line 41 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		rect.set("height", haxe.root.Std.string(( ( (( relation.fields.length + 1 )) * 16 ) + 6 )));
		//line 42 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		rect.set("width", haxe.root.Std.string(((int) (( ( chrWidth * 10.0 ) + 6 )) )));
		//line 43 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		rect.set("style", "fill:white");
		//line 45 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml titleBG = haxe.root.Xml.createElement("rect");
		//line 46 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		g.addChild(titleBG);
		//line 47 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		titleBG.set("height", "19");
		//line 48 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		titleBG.set("width", haxe.root.Std.string(( ( chrWidth * 10.0 ) + 6 )));
		//line 49 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		titleBG.set("style", "fill:white");
		//line 51 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml title = haxe.root.Xml.createElement("text");
		//line 52 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		g.addChild(title);
		//line 53 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		title.addChild(haxe.root.Xml.createPCData(relation.name));
		//line 54 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		title.set("x", haxe.root.Std.string(( 3 + ( ( (( chrWidth - relation.name.length() )) * 10.0 ) / 2 ) )));
		//line 55 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		title.set("y", "16");
		//line 57 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml text = haxe.root.Xml.createElement("text");
		//line 58 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		g.addChild(text);
		//line 59 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		{
			//line 59 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			int _g2 = 0;
			//line 59 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			int _g3 = relation.fields.length;
			//line 59 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			while (( _g2 < _g3 ))
			{
				//line 59 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				int i = _g2++;
				//line 60 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				haxe.root.Xml tspan = haxe.root.Xml.createElement("tspan");
				//line 61 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				text.addChild(tspan);
				//line 62 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				tspan.set("x", haxe.root.Std.string(1.5));
				//line 63 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				tspan.set("y", haxe.root.Std.string(( ( 16 * (( i + 2 )) ) + 3 )));
				//line 64 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				if (( relation.key.indexOf(relation.fields.__get(i).name, null) != -1 )) 
				{
					//line 65 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
					tspan.set("style", "text-decoration:underline");
				}
				
				//line 67 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				tspan.addChild(haxe.root.Xml.createPCData(relation.fields.__get(i).name));
				//line 68 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				if (typed) 
				{
					//line 69 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
					tspan = haxe.root.Xml.createElement("tspan");
					//line 70 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
					text.addChild(tspan);
					//line 71 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
					tspan.set("x", haxe.root.Std.string(( 3 + ( (( chrWidth - relation.fields.__get(i).dbType.length() )) * 10.0 ) )));
					//line 72 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
					tspan.set("y", haxe.root.Std.string(( ( 16 * (( i + 2 )) ) + 3 )));
					//line 73 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
					tspan.addChild(haxe.root.Xml.createPCData(relation.fields.__get(i).dbType));
				}
				
			}
			
		}
		
		//line 76 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		return g;
	}
	
	
	public static haxe.root.Xml generateLineSVG(model.Relation relation, java.lang.Object fk)
	{
		//line 81 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Array<java.lang.String> coords1 = haxe.lang.StringExt.split(schema.Svg.coordRE.replace(relation.svg.get("transform"), "$1"), ",");
		//line 82 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		int x1 = ((int) (haxe.lang.Runtime.toInt(haxe.root.Std.parseInt(coords1.__get(0)))) );
		//line 83 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		int y1 = ((int) (haxe.lang.Runtime.toInt(haxe.root.Std.parseInt(coords1.__get(1)))) );
		//line 84 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml _this = relation.svg;
		//line 84 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		if (( ( _this.nodeType != haxe.root.Xml.Document ) && ( _this.nodeType != haxe.root.Xml.Element ) )) 
		{
			//line 84 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			throw ((java.lang.RuntimeException) (haxe.Exception.thrown(( "Bad node type, expected Element or Document but found " + _Xml.XmlType_Impl_.toString(_this.nodeType) ))) );
		}
		
		//line 84 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		int w1 = ((int) (haxe.lang.Runtime.toInt(haxe.root.Std.parseInt(_this.children.__get(0).get("width")))) );
		//line 85 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Array<java.lang.String> coords2 = haxe.lang.StringExt.split(schema.Svg.coordRE.replace(((model.Relation) (haxe.lang.Runtime.getField(fk, "relation", true)) ).svg.get("transform"), "$1"), ",");
		//line 86 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		int x2 = ((int) (haxe.lang.Runtime.toInt(haxe.root.Std.parseInt(coords2.__get(0)))) );
		//line 87 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		int y2 = ((int) (haxe.lang.Runtime.toInt(haxe.root.Std.parseInt(coords2.__get(1)))) );
		//line 88 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml _this1 = ((model.Relation) (haxe.lang.Runtime.getField(fk, "relation", true)) ).svg;
		//line 88 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		if (( ( _this1.nodeType != haxe.root.Xml.Document ) && ( _this1.nodeType != haxe.root.Xml.Element ) )) 
		{
			//line 88 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			throw ((java.lang.RuntimeException) (haxe.Exception.thrown(( "Bad node type, expected Element or Document but found " + _Xml.XmlType_Impl_.toString(_this1.nodeType) ))) );
		}
		
		//line 88 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		int w2 = ((int) (haxe.lang.Runtime.toInt(haxe.root.Std.parseInt(_this1.children.__get(0).get("width")))) );
		//line 91 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		y1 += ( 16 * (( relation.getFieldIndex(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(fk, "name", true))) + 2 )) );
		//line 92 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		y2 += ( 16 * (( ((model.Relation) (haxe.lang.Runtime.getField(fk, "relation", true)) ).getFieldIndex(haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(fk, "key", true))) + 2 )) );
		//line 93 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		int xl1 = 0;
		//line 94 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		int xl2 = 0;
		//line 95 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		int xa = 3;
		//line 96 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		if (( relation.col == ((model.Relation) (haxe.lang.Runtime.getField(fk, "relation", true)) ).col )) 
		{
			//line 97 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			x1 += w1;
			//line 98 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			x2 += w2;
			//line 99 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			xl1 = ( ((int) (java.lang.Math.max(((double) (x1) ), ((double) (x2) ))) ) + 6 );
			//line 100 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			xl2 = xl1;
		}
		else
		{
			//line 101 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
			if (( relation.col < ((model.Relation) (haxe.lang.Runtime.getField(fk, "relation", true)) ).col )) 
			{
				//line 102 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				x1 += w1;
				//line 103 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				xl1 = ( x1 + 6 );
				//line 104 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				xl2 = ( x2 - 6 );
				//line 105 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				xa =  - (xa) ;
			}
			else
			{
				//line 107 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				x2 += w2;
				//line 108 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				xl1 = ( x1 - 6 );
				//line 109 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
				xl2 = ( x2 + 6 );
			}
			
		}
		
		//line 111 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml g = haxe.root.Xml.createElement("g");
		//line 112 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		haxe.root.Xml line = haxe.root.Xml.createElement("path");
		//line 113 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		line.set("d", ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( "M " + x1 ) + " " ) + y1 ) + " L " ) + xl1 ) + " " ) + y1 ) + " L " ) + xl2 ) + " " ) + y2 ) + " L " ) + x2 ) + " " ) + y2 ));
		//line 114 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		line.set("marker-end", ( ( "url(#" + haxe.lang.Runtime.toString(haxe.lang.Runtime.getField(schema.Svg.MARKERS.__get(0), "id", true)) ) + ")" ));
		//line 115 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		g.addChild(line);
		//line 116 "/home/frank/Documents/Developpements/dbconcept-0.5.1/src/schema/MLDToSVG.hx"
		return g;
	}
	
	
}


