using System;
using System.Collections;
using System.Data.OleDb;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;

namespace Super2d3dGraphLibrary;

public class SeriesFactory
{
	public delegate float f_y(float x, float z);

	private ArrayList[] arrSeries;

	private ArrayList[] arrToolTips;

	private bool tooltips_set;

	private ArrayList[] arrColors;

	private bool colors_set;

	private string[] arrItemsLegend;

	private bool itemsLegend_set;

	private Pen[] arrSeriesLineStyle;

	private bool seriesLineStyle_set;

	private int index;

	private string m_strFormula;

	private string[] arrValuesCaptions;

	private bool valuesCaptions_set;

	public ArrayList[] Series
	{
		get
		{
			return arrSeries;
		}
		set
		{
			arrSeries = value;
		}
	}

	public ArrayList[] ToolTips
	{
		get
		{
			return arrToolTips;
		}
		set
		{
			arrToolTips = value;
		}
	}

	public SeriesFactory()
	{
		index = -1;
		Reset();
	}

	public void Reset()
	{
		index = -1;
		arrSeries = new ArrayList[1];
		arrToolTips = new ArrayList[1];
		tooltips_set = false;
		arrColors = new ArrayList[1];
		colors_set = false;
		arrItemsLegend = new string[1];
		itemsLegend_set = false;
		arrSeriesLineStyle = new Pen[1];
		seriesLineStyle_set = false;
		arrValuesCaptions = new string[1];
		valuesCaptions_set = false;
		NewSerie();
	}

	public void NewSerie()
	{
		checked
		{
			if (arrSeries[0] == null || arrSeries[0].Count != 0)
			{
				index++;
				arrSeries = (ArrayList[])Utils.CopyArray(arrSeries, new ArrayList[index + 1]);
				arrSeries[index] = new ArrayList();
				arrToolTips = (ArrayList[])Utils.CopyArray(arrToolTips, new ArrayList[index + 1]);
				arrToolTips[index] = new ArrayList();
				arrColors = (ArrayList[])Utils.CopyArray(arrColors, new ArrayList[index + 1]);
				arrColors[index] = new ArrayList();
			}
		}
	}

	public void SerieLineStyle(Pen p)
	{
		checked
		{
			int num = arrSeries.Length - 1;
			if (num >= arrSeriesLineStyle.Length)
			{
				arrSeriesLineStyle = (Pen[])Utils.CopyArray(arrSeriesLineStyle, new Pen[num + 1]);
			}
			arrSeriesLineStyle[num] = p;
			seriesLineStyle_set = true;
		}
	}

	public void AddValueEmpty()
	{
		arrSeries[index].Add(null);
	}

	public void AddValue(double v)
	{
		arrSeries[index].Add(v);
	}

	public void AddValueCaption(string s)
	{
		int num = arrValuesCaptions.Length;
		arrValuesCaptions = (string[])Utils.CopyArray(arrValuesCaptions, new string[checked(num + 1)]);
		arrValuesCaptions[num] = s;
		valuesCaptions_set = true;
	}

	public void AddPoint(float x, float y)
	{
		arrSeries[index].Add(new float[2] { x, y });
	}

	public void AddBubble(float x, float y, float size)
	{
		arrSeries[index].Add(new float[3] { x, y, size });
	}

	public void AddToolTip(string s)
	{
		int num = arrSeries.Length;
		checked
		{
			if (arrToolTips.Length < num)
			{
				arrToolTips = (ArrayList[])Utils.CopyArray(arrToolTips, new ArrayList[num + 1]);
				int num2 = num - 1;
				for (int i = 1; i <= num2; i++)
				{
					int count = arrSeries[i].Count;
					while (arrToolTips[i].Count < count)
					{
						arrToolTips[i].Add("$$");
					}
				}
			}
			arrToolTips[index].Add(s);
			tooltips_set = true;
		}
	}

	public void ItemColor(Color c)
	{
		int num = arrSeries.Length;
		checked
		{
			if (arrColors.Length < num)
			{
				arrColors = (ArrayList[])Utils.CopyArray(arrColors, new ArrayList[num + 1]);
			}
			int count = arrSeries[num - 1].Count;
			while (arrColors[num - 1].Count < count - 1)
			{
				arrColors[num - 1].Add(-1);
			}
			arrColors[index].Add(c);
			colors_set = true;
		}
	}

	public void ItemLegend(string s)
	{
		checked
		{
			int num = arrSeries.Length - 1;
			int num2 = Math.Max(arrSeries[num].Count - 1, 0);
			if (arrItemsLegend.Length - 1 < num2)
			{
				arrItemsLegend = (string[])Utils.CopyArray(arrItemsLegend, new string[num2 + 1]);
			}
			arrItemsLegend[num2] = s;
			itemsLegend_set = true;
		}
	}

	public void AddHighLow(float min, float max)
	{
		arrSeries[index].Add(new float[2] { min, max });
	}

	public void AddHighLowClose(float min, float max, float close)
	{
		arrSeries[index].Add(new float[3] { min, max, close });
	}

	public void AddHighLowOpenClose(float min, float max, float open, float close)
	{
		arrSeries[index].Add(new float[4] { min, max, open, close });
	}

	public void AddDateTimeValue(DateTime d, float y)
	{
		arrSeries[index].Add(new object[2] { d, y });
	}

	public void AddDateTimeValueEmpty(DateTime d)
	{
		arrSeries[index].Add(new object[2] { d, null });
	}

	public void AddPoint(float x, float y, float stdDev)
	{
		arrSeries[index].Add(new float[3] { x, y, stdDev });
	}

	public void GenerateMesh()
	{
		DelaunayTriangulation delaunayTriangulation = new DelaunayTriangulation();
		delaunayTriangulation.AssignData(Series[0]);
		delaunayTriangulation.DoTriangulation();
		Series[0] = delaunayTriangulation.getPoints;
	}

	public void ApplyTo(Super2d3dGraph chart)
	{
		bool autoRefresh = chart.AutoRefresh;
		chart.AutoRefresh = false;
		chart.Series = arrSeries;
		if (tooltips_set)
		{
			chart.HotSpotTooltips = arrToolTips;
		}
		if (colors_set)
		{
			chart.ItemColors = arrColors;
		}
		if (itemsLegend_set)
		{
			chart.ItemsLegend = arrItemsLegend;
		}
		if (seriesLineStyle_set)
		{
			chart.SeriesLineStyle = arrSeriesLineStyle;
		}
		if (valuesCaptions_set)
		{
			chart.ValuesCaptions = arrValuesCaptions;
		}
		if (autoRefresh)
		{
			chart.AutoRefresh = true;
		}
		else
		{
			chart.RefreshChart();
		}
	}

	public void AddPoints(float x_min, float x_max, float x_step, float z_min, float z_max, float z_step, ref f_y y)
	{
		for (float num = x_min; ObjectFlowControl.ForLoopControl.ForNextCheckR4(num, x_max, x_step); num += x_step)
		{
			NewSerie();
			for (float num2 = z_min; ObjectFlowControl.ForLoopControl.ForNextCheckR4(num2, z_max, z_step); num2 += z_step)
			{
				AddPoint(num, y(num, num2), num2);
			}
		}
	}

	public void AddTask(DateTime startDate, DateTime endDate, float percentageDone)
	{
		arrSeries[index].Add(new object[3] { startDate, endDate, percentageDone });
	}

	public void AddTask(params object[] taskData)
	{
		arrSeries[index].Add(taskData);
	}

	public void AddPointsFromFile(string file)
	{
		string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Mode=Read;Extended Properties=Excel 8.0;";
		OleDbConnection oleDbConnection = new OleDbConnection(connectionString);
		Collection collection = new Collection();
		string cmdText = "SELECT Series, X, Y, Z FROM [Sheet1$]";
		OleDbCommand oleDbCommand = new OleDbCommand(cmdText, oleDbConnection);
		oleDbConnection.Open();
		OleDbDataReader oleDbDataReader = oleDbCommand.ExecuteReader();
		int num2 = default(int);
		while (oleDbDataReader.Read())
		{
			try
			{
				int num = Conversions.ToInteger(oleDbDataReader.GetValue(0));
				if (num > num2)
				{
					num2 = num;
				}
				float num3 = Conversions.ToSingle(oleDbDataReader.GetValue(1));
				float num4 = Conversions.ToSingle(oleDbDataReader.GetValue(2));
				float num5 = Conversions.ToSingle(oleDbDataReader.GetValue(3));
				collection.Add(new float[4] { num, num3, num4, num5 });
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				ProjectData.ClearProjectError();
			}
		}
		oleDbDataReader.Close();
		oleDbConnection.Close();
		int num6 = num2;
		for (int i = 0; i <= num6; i = checked(i + 1))
		{
			while (arrSeries.Length <= i)
			{
				NewSerie();
			}
			foreach (float[] item in collection)
			{
				if (item[0] == (float)i)
				{
					arrSeries[i].Add(new float[3]
					{
						item[1],
						item[2],
						item[3]
					});
				}
			}
		}
	}

	public void AddLine(float x0, float y0, float z0, float x1, float y1, float z1)
	{
		AddPoint(x0, y0, z0);
		AddPoint(x1, y1, z1);
	}

	private float ConvToSingle(object value)
	{
		if (Information.IsDBNull(RuntimeHelpers.GetObjectValue(value)) | Information.IsNothing(RuntimeHelpers.GetObjectValue(value)))
		{
			return 0f;
		}
		return (float)Conversion.Val(RuntimeHelpers.GetObjectValue(value));
	}

	public void AddDataFromSource(object dataSource, DATASOURCE_SCHEME dataSourceMode)
	{
		if (Operators.CompareString(dataSource.GetType().Name, "DataGridView", TextCompare: false) != 0)
		{
			throw new Exception("Data source object not supported (yet).");
		}
		DataGridView dataGridView = (DataGridView)dataSource;
		checked
		{
			switch (dataSourceMode)
			{
			case DATASOURCE_SCHEME.XZ_HEADERS_Y_CELLS:
			{
				int num3 = dataGridView.Rows.Count - 1;
				for (int k = 0; k <= num3; k++)
				{
					if (!dataGridView.Rows[k].Visible)
					{
						continue;
					}
					NewSerie();
					int num4 = dataGridView.Columns.Count - 1;
					for (int l = 0; l <= num4; l++)
					{
						if (dataGridView.Columns[l].Visible)
						{
							float x = ConvToSingle(RuntimeHelpers.GetObjectValue(dataGridView.Columns[l].HeaderCell.Value));
							float stdDev = ConvToSingle(RuntimeHelpers.GetObjectValue(dataGridView.Rows[k].HeaderCell.Value));
							float y = ConvToSingle(RuntimeHelpers.GetObjectValue(dataGridView.Rows[k].Cells[l].Value));
							AddPoint(x, y, stdDev);
						}
					}
				}
				break;
			}
			case DATASOURCE_SCHEME.CELL_VALUES:
			{
				int num = dataGridView.Columns.Count - 1;
				for (int i = 0; i <= num; i++)
				{
					if (!dataGridView.Columns[i].Visible)
					{
						continue;
					}
					NewSerie();
					int num2 = dataGridView.Rows.Count - 1;
					for (int j = 0; j <= num2; j++)
					{
						if (dataGridView.Rows[j].Visible)
						{
							AddValue(Conversions.ToDouble(dataGridView[i, j].Value));
						}
					}
				}
				break;
			}
			}
		}
	}

	protected virtual void Finalize()
	{
		base.Finalize();
	}
}
