using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Runtime.CompilerServices;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;

namespace Super2d3dGraphLibrary;

[Description("Esta clase proporciona funcionalidad para la colección de etiquetas y su renderizado según parámetros de estilo establecidos.")]
public class ValueCaptionRenderer
{
	private Collection m_colVC;

	private StringFormat m_sf;

	private STYLECAPTIONLOCATION m_locStyle;

	private double m_dblMax;

	private double m_dblMin;

	private bool m_blnHorizontalChart;

	private CustomGraphics _g;

	private double m_dblMinValue;

	private double m_dblMaxValue;

	public object ValueCaptions => m_colVC;

	public ValueCaptionRenderer(CustomGraphics g, bool blnHorizontalChart, double minValue, double maxValue)
	{
		m_blnHorizontalChart = false;
		m_dblMinValue = double.MinValue;
		m_dblMaxValue = double.MaxValue;
		m_colVC = new Collection();
		m_sf = new StringFormat();
		m_dblMax = -3.4028234663852886E+38;
		m_dblMin = 3.4028234663852886E+38;
		m_blnHorizontalChart = blnHorizontalChart;
		m_dblMinValue = minValue;
		m_dblMaxValue = maxValue;
		_g = g;
		m_sf.Alignment = StringAlignment.Center;
		m_sf.LineAlignment = StringAlignment.Center;
	}

	public void AddCaption(ValueCaption vc)
	{
		if ((vc.ValueNumeric >= m_dblMinValue) & (vc.ValueNumeric <= m_dblMaxValue))
		{
			m_colVC.Add(vc);
			if (vc.ValueNumeric > m_dblMax)
			{
				m_dblMax = vc.ValueNumeric;
			}
			if (vc.ValueNumeric < m_dblMin)
			{
				m_dblMin = vc.ValueNumeric;
			}
		}
	}

	private int m_intCollide(ref ValueCaption vc1, ref ValueCaption vc2, ref string strValueFormat, ref Font fnt, ref int yDisp)
	{
		SizeF sizeF = default(SizeF);
		if (vc1.Size == sizeF)
		{
			vc1.Size = _g.MeasureString(vc1.get_Value(strValueFormat), fnt);
		}
		SizeF size = vc1.Size;
		checked
		{
			int num = (int)Math.Round((double)size.Width * 1.1);
			int num2 = (int)Math.Round(size.Height);
			Rectangle rectangle = new Rectangle((int)Math.Round((double)vc1.X - (double)num / 2.0), (int)Math.Round((double)vc1.Y - (double)num2 / 2.0 + (double)yDisp), num, num2);
			if (vc2.Size == sizeF)
			{
				vc2.Size = _g.MeasureString(vc2.get_Value(strValueFormat), fnt);
			}
			SizeF size2 = vc2.Size;
			int num3 = (int)Math.Round((double)size2.Width * 1.1);
			int num4 = (int)Math.Round(size2.Height);
			RectangleF rectangleF = new RectangleF((float)((double)vc2.X - (double)num3 / 2.0), (float)((double)vc2.Y - (double)num4 / 2.0), num3, num4);
			if (rectangleF.IntersectsWith(rectangle))
			{
				num2 = (int)Math.Round(Math.Abs((float)rectangle.Bottom - rectangleF.Top));
				num = (int)Math.Round(Math.Abs(rectangleF.Bottom - (float)rectangle.Top));
				if (num > num2)
				{
					num2 = num;
				}
				return num2 + 5;
			}
			return 0;
		}
	}

	private bool m_blnSafeMove(ref int vcIndex, ref string strValueFormat, ref Font fnt, ref int yDisp)
	{
		int count = m_colVC.Count;
		for (int i = 1; i <= count; i = checked(i + 1))
		{
			if (i != vcIndex)
			{
				ValueCaption vc = (ValueCaption)m_colVC[vcIndex];
				ValueCaption vc2 = (ValueCaption)m_colVC[i];
				if (m_intCollide(ref vc, ref vc2, ref strValueFormat, ref fnt, ref yDisp) != 0)
				{
					return false;
				}
			}
		}
		return true;
	}

	public void HighlightSelection(Super2d3dGraph chart)
	{
		checked
		{
			foreach (ValueCaption item in chart.ItemsSelected)
			{
				foreach (ValueCaption item2 in m_colVC)
				{
					if ((item.seriesIndex == item2.seriesIndex) & (item.itemIndex == item2.itemIndex))
					{
						if (item2.RECT.Width != 0)
						{
							_g.DrawPath(chart.HotSpotOutlineStyle, item2.PATH);
							continue;
						}
						CustomGraphics g = _g;
						SolidBrush bs = new SolidBrush(chart.HotSpotOutlineStyle.Color);
						Rectangle r = new Rectangle((int)Math.Round((double)item2.X_HotSpot - (double)chart.PointSize / 2.0 - 1.0), (int)Math.Round((double)item2.Y_HotSpot - (double)chart.PointSize / 2.0 - 1.0), chart.PointSize + 2, chart.PointSize + 2);
						g.FillEllipse(bs, r);
						CustomGraphics g2 = _g;
						Pen pen = new Pen(Color.Black);
						r = new Rectangle((int)Math.Round((double)item2.X_HotSpot - (double)chart.PointSize / 2.0 - 1.0), (int)Math.Round((double)item2.Y_HotSpot - (double)chart.PointSize / 2.0 - 1.0), chart.PointSize + 2, chart.PointSize + 2);
						g2.DrawEllipse(pen, r);
					}
				}
			}
		}
	}

	public void Render(bool blnShowDisplacementLine, Rectangle rectChart, ArrayList itemsSelected, ref Super2d3dGraph chart)
	{
		int num = int.MaxValue;
		Pen pen = new Pen(Color.Black);
		pen.DashStyle = DashStyle.Dash;
		if ((chart.ValuesLocationStyle == STYLECAPTIONLOCATION.STYLECAPTIONLOCATION_INSIDE) & chart.IsPie)
		{
			chart.ValuesLocationStyle = STYLECAPTIONLOCATION.STYLECAPTIONLOCATION_SMART_PLACEMENT;
		}
		checked
		{
			if (chart.ValuesLocationStyle == STYLECAPTIONLOCATION.STYLECAPTIONLOCATION_SMART_PLACEMENT)
			{
				int num2 = 0;
				Font fnt = new Font(chart.ValuesFont, FontStyle.Regular);
				float height = _g.MeasureString("0", fnt).Height;
				bool flag;
				float num10 = default(float);
				do
				{
					int num3 = 0;
					Collection collection = new Collection();
					flag = false;
					num2++;
					int count = m_colVC.Count;
					for (int i = 1; i <= count; i++)
					{
						object instance = m_colVC[i];
						NewLateBinding.LateSetComplex(instance, null, "X_OLD", new object[1] { RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(instance, null, "X", new object[0], null, null, null)) }, null, null, OptimisticSet: false, RValueBase: true);
						NewLateBinding.LateSetComplex(instance, null, "Y_OLD", new object[1] { RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(instance, null, "Y", new object[0], null, null, null)) }, null, null, OptimisticSet: false, RValueBase: true);
						instance = null;
					}
					int num4 = m_colVC.Count - 1;
					for (int j = 1; j <= num4; j++)
					{
						ValueCaption vc = (ValueCaption)m_colVC[j];
						string text = vc.get_Value(chart.ValuesFormat);
						if (chart.ValuesCaptions.Length > j)
						{
							text = chart.ValuesCaptions[j].Replace("$$", text);
						}
						if (Operators.CompareString(text, "", TextCompare: false) == 0)
						{
							continue;
						}
						if (vc.Y < (float)rectChart.Top)
						{
							vc.sngYDisplacementDirection = 1f;
						}
						else if (vc.Y > (float)rectChart.Bottom - height)
						{
							vc.Y = (float)rectChart.Bottom - height - (float)chart.PointSize - 5f;
							vc.sngYDisplacementDirection = -1f;
						}
						int num5 = j + 1;
						int count2 = m_colVC.Count;
						for (int k = num5; k <= count2; k++)
						{
							ValueCaption vc2 = (ValueCaption)m_colVC[k];
							Super2d3dGraph super2d3dGraph = chart;
							string strValueFormat = super2d3dGraph.ValuesFormat;
							int yDisp = 0;
							int num6 = m_intCollide(ref vc, ref vc2, ref strValueFormat, ref fnt, ref yDisp);
							super2d3dGraph.ValuesFormat = strValueFormat;
							int yDisp2 = num6;
							if (yDisp2 == 0)
							{
								continue;
							}
							num3++;
							flag = true;
							bool num7 = vc.sngYDisplacementDirection > 0f;
							super2d3dGraph = chart;
							strValueFormat = super2d3dGraph.ValuesFormat;
							bool num8 = m_blnSafeMove(ref j, ref strValueFormat, ref fnt, ref yDisp2);
							super2d3dGraph.ValuesFormat = strValueFormat;
							if (unchecked(num7 && num8))
							{
								vc.MoveTo(vc.X, vc.Y + (float)yDisp2);
								vc.ShowDisplacementLine = true;
								if (vc.X < (float)rectChart.Left)
								{
									vc.X = rectChart.Left;
								}
								else if (vc.X > (float)rectChart.Right)
								{
									vc.X = rectChart.Right;
								}
								if (vc.Y < (float)rectChart.Top)
								{
									vc.sngYDisplacementDirection = 1f;
								}
								else if (vc.Y > (float)rectChart.Bottom - height)
								{
									vc.Y = (float)rectChart.Bottom - height - (float)chart.PointSize - 5f;
									vc.sngYDisplacementDirection = -1f;
								}
								continue;
							}
							object left = Operators.CompareObjectLess(NewLateBinding.LateGet(m_colVC[k], null, "sngYDisplacementDirection", new object[0], null, null, null), 0, TextCompare: false);
							super2d3dGraph = chart;
							strValueFormat = super2d3dGraph.ValuesFormat;
							yDisp = -yDisp2;
							bool num9 = m_blnSafeMove(ref k, ref strValueFormat, ref fnt, ref yDisp);
							super2d3dGraph.ValuesFormat = strValueFormat;
							if (Conversions.ToBoolean(Operators.AndObject(left, num9)))
							{
								object instance2 = m_colVC[k];
								object[] array = new object[2];
								object[] array2 = array;
								object instance3 = m_colVC[k];
								array2[0] = RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(instance3, null, "x", new object[0], null, null, null));
								array[1] = Operators.SubtractObject(NewLateBinding.LateGet(m_colVC[k], null, "Y", new object[0], null, null, null), yDisp2);
								object[] array3 = array;
								bool[] array4 = new bool[2] { true, false };
								NewLateBinding.LateCall(instance2, null, "MoveTo", array3, null, null, array4, IgnoreReturn: true);
								if (array4[0])
								{
									NewLateBinding.LateSetComplex(instance3, null, "x", new object[1] { RuntimeHelpers.GetObjectValue(array3[0]) }, null, null, OptimisticSet: true, RValueBase: true);
								}
								NewLateBinding.LateSetComplex(m_colVC[k], null, "ShowDisplacementLine", new object[1] { true }, null, null, OptimisticSet: false, RValueBase: true);
								if (Operators.ConditionalCompareObjectLess(NewLateBinding.LateGet(m_colVC[k], null, "X", new object[0], null, null, null), rectChart.Left, TextCompare: false))
								{
									NewLateBinding.LateSetComplex(m_colVC[k], null, "X", new object[1] { rectChart.Left }, null, null, OptimisticSet: false, RValueBase: true);
								}
								else if (Operators.ConditionalCompareObjectGreater(NewLateBinding.LateGet(m_colVC[k], null, "X", new object[0], null, null, null), rectChart.Right, TextCompare: false))
								{
									NewLateBinding.LateSetComplex(m_colVC[k], null, "X", new object[1] { rectChart.Right }, null, null, OptimisticSet: false, RValueBase: true);
								}
								if (Operators.ConditionalCompareObjectLess(NewLateBinding.LateGet(m_colVC[k], null, "Y", new object[0], null, null, null), rectChart.Top, TextCompare: false))
								{
									NewLateBinding.LateSetComplex(m_colVC[k], null, "sngYDisplacementDirection", new object[1] { 1 }, null, null, OptimisticSet: false, RValueBase: true);
								}
								else if (Operators.ConditionalCompareObjectGreater(NewLateBinding.LateGet(m_colVC[k], null, "Y", new object[0], null, null, null), (float)rectChart.Bottom - height, TextCompare: false))
								{
									NewLateBinding.LateSetComplex(m_colVC[k], null, "Y", new object[1] { (float)rectChart.Bottom - height - (float)chart.PointSize - 5f }, null, null, OptimisticSet: false, RValueBase: true);
									NewLateBinding.LateSetComplex(m_colVC[k], null, "sngYDisplacementDirection", new object[1] { -1 }, null, null, OptimisticSet: false, RValueBase: true);
								}
							}
							else
							{
								if (vc.sngXDisplacementDirection > 0f)
								{
									num10 = VBMath.Rnd() * 10f;
								}
								else if (vc.sngXDisplacementDirection < 0f)
								{
									num10 = (0f - VBMath.Rnd()) * 10f;
								}
								if (vc.sngYDisplacementDirection > 0f)
								{
									vc.MoveTo(vc.X + num10, vc.Y + (float)yDisp2);
								}
								else
								{
									vc.MoveTo(vc.X + num10, vc.Y - (float)yDisp2);
								}
								vc.ShowDisplacementLine = true;
								if (vc.X < (float)rectChart.Left)
								{
									vc.X = rectChart.Left;
								}
								else if (vc.X > (float)rectChart.Right)
								{
									vc.X = rectChart.Right;
								}
								if (vc.Y < (float)rectChart.Top)
								{
									vc.sngYDisplacementDirection = 1f;
								}
								else if (vc.Y > (float)rectChart.Bottom - height)
								{
									vc.Y = (float)rectChart.Bottom - height - (float)chart.PointSize - 5f;
									vc.sngYDisplacementDirection = -1f;
								}
							}
						}
					}
					if (num3 < num)
					{
						num = num3;
						int count3 = m_colVC.Count;
						for (int i = 1; i <= count3; i++)
						{
							object instance4 = m_colVC[i];
							NewLateBinding.LateSetComplex(instance4, null, "X_BestSoFar", new object[1] { RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(instance4, null, "X_Old", new object[0], null, null, null)) }, null, null, OptimisticSet: false, RValueBase: true);
							NewLateBinding.LateSetComplex(instance4, null, "Y_BestSoFar", new object[1] { RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(instance4, null, "Y_Old", new object[0], null, null, null)) }, null, null, OptimisticSet: false, RValueBase: true);
							instance4 = null;
						}
					}
				}
				while (unchecked(flag && num2 < 50));
				int count4 = m_colVC.Count;
				for (int i = 1; i <= count4; i++)
				{
					object instance5 = m_colVC[i];
					NewLateBinding.LateSetComplex(instance5, null, "X", new object[1] { RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(instance5, null, "X_BestSoFar", new object[0], null, null, null)) }, null, null, OptimisticSet: false, RValueBase: true);
					NewLateBinding.LateSetComplex(instance5, null, "Y", new object[1] { RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(instance5, null, "Y_BestSoFar", new object[0], null, null, null)) }, null, null, OptimisticSet: false, RValueBase: true);
					instance5 = null;
				}
			}
			int count5 = m_colVC.Count;
			bool flag2 = default(bool);
			for (int i = 1; i <= count5; i++)
			{
				ValueCaption vc = (ValueCaption)m_colVC[i];
				string text = vc.get_Value(chart.ValuesFormat);
				if (chart.ValuesCaptions.Length > i)
				{
					text = chart.ValuesCaptions[i].Replace("$$", text);
				}
				if (Operators.CompareString(text, "", TextCompare: false) == 0)
				{
					continue;
				}
				Font fnt;
				RectangleF rf;
				switch (chart.ValuesLocationStyle)
				{
				case STYLECAPTIONLOCATION.STYLECAPTIONLOCATION_INSIDE:
				{
					fnt = new Font(chart.ValuesFont, FontStyle.Italic);
					SizeF sizeF = _g.MeasureString(text, fnt);
					float num10 = sizeF.Width;
					float height = sizeF.Height;
					if (!m_blnHorizontalChart)
					{
						rf = ((vc.ValueNumeric >= 0.0) ? ((!((float)(vc.RECT.Top + 5) + num10 > (float)vc.RECT.Bottom)) ? new RectangleF(vc.X - height / 2f, vc.RECT.Top + 5, num10, height) : new RectangleF(vc.X - height / 2f, (float)(vc.RECT.Top - 5) - num10, num10, height)) : ((!((float)(vc.RECT.Bottom - 5) - num10 < (float)vc.RECT.Top)) ? new RectangleF(vc.X - height / 2f, (float)(vc.RECT.Bottom - 5) - num10, num10, height) : new RectangleF(vc.X - height / 2f, vc.RECT.Bottom + 5, num10, height)));
						flag2 = true;
					}
					else
					{
						rf = ((!(vc.ValueNumeric >= 0.0)) ? ((!((float)(vc.RECT.Left + 5) + num10 > (float)vc.RECT.Right)) ? new RectangleF(vc.RECT.Left + 5, vc.Y - height / 2f, num10, height) : new RectangleF((float)(vc.RECT.Left - 5) - num10, vc.Y - height / 2f, num10, height)) : ((!((float)(vc.RECT.Right - 5) - num10 < (float)vc.RECT.Left)) ? new RectangleF((float)(vc.RECT.Right - 5) - num10, vc.Y - height / 2f, num10, height) : new RectangleF(vc.RECT.Right + 5, vc.Y - height / 2f, num10, height)));
					}
					break;
				}
				case STYLECAPTIONLOCATION.STYLECAPTIONLOCATION_SMART_PLACEMENT:
				{
					fnt = ((chart.MarkHighest & (vc.ValueNumeric == m_dblMax)) ? new Font(chart.ValuesFont, FontStyle.Bold) : ((!(chart.MarkLowest & (vc.ValueNumeric == m_dblMin))) ? new Font(chart.ValuesFont, FontStyle.Regular) : new Font(chart.ValuesFont, FontStyle.Bold)));
					SizeF sizeF = _g.MeasureString(text, fnt);
					float num10 = (float)((double)sizeF.Width * 1.1);
					float height = sizeF.Height;
					rf = new RectangleF(vc.X - num10 / 2f, vc.Y - height, num10, height);
					if ((vc.Displacement > 15) & (blnShowDisplacementLine | vc.ShowDisplacementLine))
					{
						_g.DrawLine(pen, vc.X_HotSpot, vc.Y_HotSpot, vc.X, vc.Y);
					}
					break;
				}
				default:
				{
					fnt = ((chart.MarkHighest & (vc.ValueNumeric == m_dblMax)) ? new Font(chart.ValuesFont, FontStyle.Bold) : ((!(chart.MarkLowest & (vc.ValueNumeric == m_dblMin))) ? new Font(chart.ValuesFont, FontStyle.Regular) : new Font(chart.ValuesFont, FontStyle.Bold)));
					SizeF sizeF = _g.MeasureString(text, fnt);
					float num10 = (float)((double)sizeF.Width * 1.1);
					float height = sizeF.Height;
					rf = new RectangleF(vc.X - num10 / 2f, vc.Y - height / 2f, num10, height);
					break;
				}
				}
				Color color;
				Color color2;
				if (chart.MarkHighest & (vc.ValueNumeric == m_dblMax))
				{
					color = Color.Yellow;
					color2 = Color.Blue;
				}
				else if (chart.MarkLowest & (vc.ValueNumeric == m_dblMin))
				{
					color = Color.Red;
					color2 = Color.Yellow;
				}
				else
				{
					color = Color.FromArgb(chart.ValuesBackColor.A, chart.ValuesBackColor.R, chart.ValuesBackColor.G, chart.ValuesBackColor.B);
					color2 = Color.FromArgb(chart.ValuesColor.A, chart.ValuesColor.R, chart.ValuesColor.G, chart.ValuesColor.B);
				}
				if (chart.ValuesStyle == STYLECAPTION.STYLECAPTION_ROUNDED_SQUARE)
				{
					RoundedRectangle roundedRectangle;
					if (!m_blnHorizontalChart & (chart.ValuesLocationStyle == STYLECAPTIONLOCATION.STYLECAPTIONLOCATION_INSIDE))
					{
						CustomGraphics g = _g;
						RectangleF rf2 = new RectangleF(rf.Left, rf.Top, rf.Height, rf.Width);
						roundedRectangle = new RoundedRectangle(g, rf2, 0.5f);
					}
					else
					{
						roundedRectangle = new RoundedRectangle(_g, rf, 0.5f);
					}
					roundedRectangle.Render(color2, color);
				}
				if (!m_blnHorizontalChart & (chart.ValuesLocationStyle == STYLECAPTIONLOCATION.STYLECAPTIONLOCATION_INSIDE))
				{
					_g.TranslateTransform(rf.Left, rf.Top);
					_g.RotateTransform(-90f);
					rf = ((!m_blnHorizontalChart) ? new RectangleF(0f - rf.Width, 1f, rf.Width, rf.Height) : new RectangleF(0f, 0f, rf.Width, rf.Height));
				}
				if (chart.ValuesStyle == STYLECAPTION.STYLECAPTION_SHADOWED)
				{
					RectangleF rf3 = new RectangleF(rf.Left + 1f, rf.Top + 1f, rf.Width, rf.Height);
					_g.DrawString(text, fnt, new SolidBrush(color), rf3, m_sf);
				}
				_g.DrawString(text, fnt, new SolidBrush(color2), rf, m_sf);
				if (flag2)
				{
					_g.ResetTransform();
				}
			}
		}
	}

	public ValueCaption HitTest(int x, int y, int precision)
	{
		foreach (ValueCaption item in m_colVC)
		{
			if (item.Contains(x, y))
			{
				return item;
			}
			if (item.RECT.Width == 0 && Math.Sqrt(Math.Pow(item.X_HotSpot - (float)x, 2.0) + Math.Pow(item.Y_HotSpot - (float)y, 2.0)) < (double)precision)
			{
				return item;
			}
		}
		return null;
	}

	public ValueCaption GetByItemIndex(int seriesIndex, int itemIndex)
	{
		foreach (ValueCaption item in m_colVC)
		{
			if ((item.itemIndex == itemIndex) & (item.seriesIndex == seriesIndex))
			{
				return item;
			}
		}
		return null;
	}
}
