﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OcadMapLibrary {

	/*interface IDepth {

		int Depth {
			get;
		}

	}

	public class DepthComparer : IComparer<IDepth> {

		public int Compare(IDepth x, IDepth y) {
			return x.Depth.CompareTo(y.Depth);
		}

	}*/


}


		/*public MapColor GetMapColor(List<MapColor> mapColors) {
			for (int i = 0; i < mapColors.Count; i++) {
				if (mapColors[i].ID.Equals(mapColorID)) {
					return mapColors[i];
				}
			}
			return null;
		}*/


						/*if (numDashes < 0) {
					endLength = totalLength / 2;
					if (DashEndGap > 0) {
						double endDashLength = (endLength - DashEndGap) / 2;
						if (endDashLength > 0) {
							dashStyle.Dashes = new DoubleCollection(new double[] { endDashLength / Thickness, DashEndGap / Thickness, endDashLength * 2 / Thickness, DashEndGap / Thickness });
							geometry = data.GetGeometry();
							drawingContext.DrawGeometry(null, pen, geometry);
						}
					} else {
						geometry = data.GetGeometry();
						drawingContext.DrawGeometry(null, pen, geometry);
					}
					return;
				}*/
				/*if (numDashes == 0) {
					endLength = (totalLength - DashMainGap) / 2;
					pen = GetPen(brush);
					dashStyle = new DashStyle();
					pen.DashStyle = dashStyle;
					if (DashEndGap > 0) {
						double endDashLength = (endLength - DashEndGap) / 2;
						if (endDashLength > 0) {
							dashStyle.Dashes = new DoubleCollection(new double[] { endDashLength / Thickness, DashEndGap / Thickness, endDashLength / Thickness, DashMainGap / Thickness });
							geometry = data.GetGeometry();
							drawingContext.DrawGeometry(null, pen, geometry);
						}
					} else {
						dashStyle.Dashes = new DoubleCollection(new double[] { endLength / Thickness, DashMainGap / Thickness });
						geometry = data.GetGeometry();
						drawingContext.DrawGeometry(null, pen, geometry);
					}
					return;
				}*/
				/*double scaleFactor;
				scaleFactor = (totalLength - DashMainGap * (numDashes + 1)) / (endLength * 2 + mainLength * numDashes);
				if (scaleFactor < 0) {
					return;
				}
				mainLength *= scaleFactor;
				endLength *= scaleFactor;
				MapPathFigure midFigure = data;
				if (endLength != 0) {
					List<MapPathFigure> mapPathFigures;
					mapPathFigures = data.SplitAtLength(endLength);
					MapPathFigure startFigure = mapPathFigures[0];
					mapPathFigures = mapPathFigures[1].SplitAtLength(totalLength - endLength * 2);
					midFigure = mapPathFigures[0];
					MapPathFigure endFigure = mapPathFigures[1];
					if (DashEndGap > 0) {
						dashStyle = new DashStyle();
						pen.DashStyle = dashStyle;
						double endDashLength = (endLength - DashEndGap) / 2;
						if (endDashLength > 0) {
							dashStyle.Dashes = new DoubleCollection(new double[] { endDashLength / Thickness, DashEndGap / Thickness });
							geometry = startFigure.GetGeometry();
							drawingContext.DrawGeometry(null, pen, geometry);
							geometry = endFigure.GetGeometry();
							drawingContext.DrawGeometry(null, pen, geometry);
						}
					} else {
						geometry = startFigure.GetGeometry();
						drawingContext.DrawGeometry(null, pen, geometry);
						geometry = endFigure.GetGeometry();
						drawingContext.DrawGeometry(null, pen, geometry);
					}
				}
				geometry = midFigure.GetGeometry();
				pen = GetPen(brush);
				dashStyle = new DashStyle();
				pen.DashStyle = dashStyle;
				if (DashSecondaryGap > 0) {
					double mainDashLength = (mainLength - DashSecondaryGap) / 2;
					if (mainDashLength > 0) {
						dashStyle.Dashes = new DoubleCollection(new double[] { mainDashLength / Thickness, DashSecondaryGap / Thickness, mainDashLength / Thickness, DashMainGap / Thickness });
						dashStyle.Offset = mainLength / Thickness;
						drawingContext.DrawGeometry(null, pen, geometry);
					}
				} else {
					dashStyle.Dashes = new DoubleCollection(new double[] { mainLength / Thickness, DashMainGap / Thickness });
					dashStyle.Offset = mainLength / Thickness;
					drawingContext.DrawGeometry(null, pen, geometry);
				}*/



				/*using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace OcadMapLibrary {

	public class MapGeometryDrawing {

		MapColorRef fillColor;
		public MapColorRef FillColor {
			get {
				return fillColor;
			}
			set {
				fillColor = value;
			}
		}

		MapColorRef strokeColor;
		public MapColorRef StrokeColor {
			get {
				return strokeColor;
			}
			set {
				strokeColor = value;
			}
		}

		double strokeThickness;
		public double StrokeThickness {
			get {
				return strokeThickness;
			}
			set {
				strokeThickness = value;
			}
		}

		Geometry geometry;
		public Geometry Geometry {
			get {
				return geometry;
			}
			set {
				geometry = value;
			}
		}

		public bool ContainsColor(MapColor mapColor) {
			if (FillColor != null) {
				if (FillColor.Matches(mapColor)) {
					return true;
				}
			}
			if (StrokeColor != null && StrokeThickness > 0) {
				if (StrokeColor.Matches(mapColor)) {
					return true;
				}
			}
			return false;
		}

		public void Draw(DrawingContext drawingContext, MapColor mapColor) {
			if (Geometry == null) {
				return;
			}
			if (FillColor != null) {
				if (FillColor.Matches(mapColor)) {
					Brush brush = new SolidColorBrush(mapColor.Color);
					drawingContext.DrawGeometry(brush, null, Geometry.CloneCurrentValue());
				}
			}
			if (StrokeColor != null && StrokeThickness > 0) {
				if (StrokeColor.Matches(mapColor)) {
					Pen pen = new Pen(new SolidColorBrush(mapColor.Color), StrokeThickness);
					drawingContext.DrawGeometry(null, pen, Geometry.CloneCurrentValue());
				}
			}
		}

		public Rect GetBoundingRect(Point position) {
			Rect rect = Rect.Empty;
			if (StrokeColor != null && StrokeThickness > 0) {
				rect = Geometry.GetRenderBounds(new Pen(Brushes.Black, StrokeThickness));
			} else if (FillColor != null) {
				rect = Geometry.Bounds;
			}
			if (!rect.IsEmpty) {
				rect.Offset(position.X, position.Y);
			}
			return rect;
		}
		
	}

}
*/


Rect DrawSymbolOrGetBounds(bool doDrawing, string text, Rect layoutRect, double rotation, DrawingContext drawingContext, MapColor mapColor) {
			if (IsHidden) {
				return Rect.Empty;
			}
			if (string.IsNullOrWhiteSpace(text)) {
				return Rect.Empty;
			}
			bool matchesTextColor = false;
			bool matchesLineColor = false;
			if (mapColor != null) {
				matchesTextColor = TextColor.Matches(mapColor);
				if (LineBelowColor != null && LineBelowWidth > 0) {
					matchesLineColor = LineBelowColor.Matches(mapColor);
				}
			}
			if (!(matchesLineColor || matchesTextColor || doDrawing == false)) {
				return Rect.Empty;
			}
			double lineBelowLength = 0;
			if (matchesLineColor) {
				lineBelowLength = DrawSymbolOrGetBounds(false, text, layoutRect, rotation, null, null).Width;
			}
			string newText = text;
			if (newText.StartsWith("\r")) {
				newText = newText.Substring(1);
			}
			if (newText.StartsWith("\n")) {
				newText = newText.Substring(1);
			}
			Color newColor = Colors.Black;
			if (mapColor != null) {
				newColor = mapColor.Color;
			}
			double topMargin = 0;
			double topOffset = 0;
			if (layoutRect.Width == 0) {
				topOffset = -LineHeight * 0.8;//Unknown value
			}
			if (rotation != 0 && doDrawing == true) {
				RotateTransform rotate = new RotateTransform(rotation, layoutRect.Left, layoutRect.Top + LineHeight * 0.8);//0.8 is unknown value
				drawingContext.PushTransform(rotate);
			}
			double maxWidth = layoutRect.Width;
			string[] lines = newText.Split(new string[] { "\r\n" }, StringSplitOptions.None);
			for (int i = 0; i < lines.Length; i++) {
				string lineText = lines[i];
				double leftOffset = 0;
				if (ColumnWidths != null && lineText.IndexOf("\t") >= 0) {
					string[] textFragments = lineText.Split(new char[] { '\t' });
					double lastWidth = 0;
					double currentColWidth = 0;
					double lastColumnWidth = ColumnWidths[ColumnWidths.Count - 1];
					double currentMargin = 0;
					int currentCol = 0;
					for (int j = 0; j < textFragments.Length; j++) {
						if (j > 0) {
							currentMargin = -lastWidth;
							do {
								if (currentCol < ColumnWidths.Count) {
									currentColWidth = ColumnWidths[currentCol];
								} else {
									currentColWidth = lastColumnWidth;
								}
								currentCol++;
								currentMargin += currentColWidth;
							} while (currentMargin < 0);
						} else {
							currentMargin = 0;
						}
						FormattedText currentText = GetFormattedText(textFragments[j], newColor, layoutRect.Width);
						lastWidth = currentText.WidthIncludingTrailingWhitespace;
						Point topLeft = layoutRect.TopLeft;
						topLeft.X = topLeft.X + leftOffset + currentMargin;
						topLeft.Y = topLeft.Y + topMargin + topOffset;
						leftOffset += lastWidth + currentMargin;
						if (matchesTextColor) {
							drawingContext.DrawText(currentText, topLeft);
						}
						if (leftOffset > maxWidth) {
							maxWidth = leftOffset;
						}
					}
				} else {
					FormattedText currentText = GetFormattedText(lineText.Replace("\t", ""), newColor, layoutRect.Width);
					Point topLeft = layoutRect.TopLeft;
					topLeft.X = topLeft.X + leftOffset;
					topLeft.Y = topLeft.Y + topMargin + topOffset;
					if (matchesTextColor) {
						drawingContext.DrawText(currentText, topLeft);
					}
					double width = currentText.WidthIncludingTrailingWhitespace;
					if (width > maxWidth) {
						maxWidth = width;
					}
				}
				topOffset += topMargin + LineHeight;
				if (LineBelowColor != null && LineBelowWidth>0) {
					if (matchesLineColor) {
						double lineY = layoutRect.Top+topOffset+0.2 * LineBelowOffset;//0.2 is unknown value
						drawingContext.DrawLine(new Pen(new SolidColorBrush(newColor), LineBelowWidth), new Point(layoutRect.Left, lineY), new Point(layoutRect.Left + lineBelowLength, lineY));
					}
					topOffset += LineBelowWidth + 0.2 * LineBelowOffset;//0.2 is unknown value
				}
				topMargin = ParagraphSpace;
			}
			if (rotation != 0 && doDrawing == true) {
				drawingContext.Pop();
			}
			Rect rect = layoutRect;
			rect.Width = maxWidth;
			rect.Height = topOffset;
			if (layoutRect.Width == 0) {
				rect.Height = rect.Height + LineHeight * 0.8;//Unknown value
				rect.Y = rect.Y - LineHeight * 0.8;//Unknown value
				if (Alignment == TextAlignment.Center) {
					rect.X = rect.X - rect.Width / 2;
				}else if (Alignment == TextAlignment.Right) {
					rect.X = rect.X - rect.Width;
				}
			}
			if (rotation != 0) {
				RotateTransform rotate = new RotateTransform(rotation, layoutRect.Left, layoutRect.Top + LineHeight * 0.8);//0.8 is unknown value);
				rect = rotate.TransformBounds(rect);
			}
			return rect;
		}

				double characterSpace;
		public double CharacterSpace {
			get {
				return characterSpace;
			}
			set {
				characterSpace = value;
			}
		}