using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace WindowsFormsApplication2;

public class BsvReader
{
	private bool hasHeader;

	private string Plot_Title;

	private string Plot_X_Label;

	private string Plot_Y_Label;

	private List<string> Plot_Series_Titles = new List<string>();

	private string Plot_Constellation;

	protected StreamReader bsvFileStreamReader;

	private string lineFromPeek = "";

	private string lastLineRead = "";

	private Action<string> logger;

	public bool HasHeader
	{
		get
		{
			return hasHeader;
		}
		set
		{
			hasHeader = value;
		}
	}

	public BsvReader(StreamReader sr, bool hasHeaders)
	{
		bsvFileStreamReader = sr;
		hasHeader = hasHeaders;
	}

	public void SetLogger(Action<string> loggerAction)
	{
		logger = loggerAction;
	}

	protected virtual void WriteLog(string msg)
	{
		if (logger != null)
		{
			logger(msg);
		}
	}

	public virtual string Get_Basic_Plot_Headers(StreamReader qsr)
	{
		Plot_Title = qsr.ReadLine();
		Plot_X_Label = qsr.ReadLine();
		Plot_Y_Label = qsr.ReadLine();
		string text = qsr.ReadLine();
		if (text != "")
		{
			char[] separator = new char[1] { ';' };
			string[] source = text.Split(separator);
			Plot_Series_Titles = source.ToList();
		}
		else
		{
			Plot_Series_Titles.Add(Plot_Title);
		}
		Plot_Constellation = qsr.ReadLine();
		string text2;
		do
		{
			text2 = qsr.ReadLine();
		}
		while (!text2.StartsWith("***") && !ReachedEndOfFile());
		return text2;
	}

	public virtual string GetPlotTitle()
	{
		return Plot_Title;
	}

	public virtual string GetPlot_X_Label()
	{
		return Plot_X_Label;
	}

	public virtual string GetPlot_Y_Label()
	{
		return Plot_Y_Label;
	}

	public virtual string GetPlot_Series_Title(int Index)
	{
		return Plot_Series_Titles[Index];
	}

	public virtual string GetPlot_Constellation()
	{
		return Plot_Constellation;
	}

	protected virtual List<string> ParsebsvDelimitedLine(string line)
	{
		List<string> result = new List<string>();
		if (!string.IsNullOrEmpty(line))
		{
			try
			{
				string[] array = line.Split(new string[1] { " " }, StringSplitOptions.None);
				if (array != null && array.Length > 0)
				{
					result = array.ToList();
				}
			}
			catch (Exception ex)
			{
				WriteLog(ex.ToString());
			}
		}
		return result;
	}

	protected virtual List<string> ParseSpaceDelimitedLine(string line)
	{
		List<string> result = new List<string>();
		if (!string.IsNullOrEmpty(line))
		{
			try
			{
				string[] array = line.Split(new string[1] { " " }, StringSplitOptions.None);
				if (array != null && array.Length > 0)
				{
					result = array.ToList();
				}
			}
			catch (Exception ex)
			{
				WriteLog(ex.ToString());
			}
		}
		return result;
	}

	public bool ReachedEndOfFile()
	{
		if (bsvFileStreamReader != null && bsvFileStreamReader.Peek() <= 0)
		{
			return true;
		}
		return false;
	}

	public virtual List<string> ReadNextLine()
	{
		if (!ReachedEndOfFile())
		{
			if (lineFromPeek != "")
			{
				string line = lineFromPeek;
				lineFromPeek = "";
				lastLineRead = line;
				return ParsebsvDelimitedLine(line);
			}
			lineFromPeek = "";
			lastLineRead = bsvFileStreamReader.ReadLine();
			return ParsebsvDelimitedLine(lastLineRead);
		}
		return null;
	}

	public virtual string ReadLine()
	{
		if (!ReachedEndOfFile())
		{
			if (lineFromPeek != "")
			{
				string result = lineFromPeek;
				lineFromPeek = "";
				lastLineRead = result;
				return result;
			}
			lineFromPeek = "";
			lastLineRead = bsvFileStreamReader.ReadLine();
			return lastLineRead;
		}
		return null;
	}

	public virtual string PeekLine()
	{
		if (!ReachedEndOfFile() && lineFromPeek == "")
		{
			lineFromPeek = bsvFileStreamReader.ReadLine();
		}
		return lineFromPeek;
	}

	public virtual string LastLineRead()
	{
		return lastLineRead;
	}
}
