using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Threading.Tasks;

namespace MarketData.CSVHelper
{
  public class CSVFileExt2 : CSVFileExt
  {
    public CSVFileExt2()
    {
    }
    public override Dictionary<String, String> LoadCSVFile(StreamReader streamReader,int startDataRow)
    {
      try
      {
        DateTime modified = DateTime.Now;
//        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Loading CSV File from stream"));
        String strLine = null;
        ReadHeaderRows(streamReader, startDataRow);
        strLine = streamReader.ReadLine();
        CreateColumns(strLine);
        int rowCount = 0;
        while (null != (strLine = streamReader.ReadLine()))
        {
          if ("".Equals(strLine)) continue;
          String[] rowData = StringParser.ParseDelimitedString(strLine);
          if (1 == rowData.Length && rowData[0].Trim().ToUpper().Equals("**NOTE :")) break;
          List<String> rowDataList = rowData.ToList();
          rows.Add(rowDataList);
          rowCount++;
        }
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Loaded {0} rows from stream", rows.Count));
        return header;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString());
        return null;
      }
      finally
      {
      }
    }
    public override Dictionary<String, String> LoadCSVFile(String strPathFileName, int startDataRow)
    {
      StreamReader streamReader = null;
      try
      {
        DateTime modified = DateTime.Now;
//        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Loading CSV File {0}", strPathFileName));
        streamReader = File.OpenText(strPathFileName);
        String strLine = null;
        ReadHeaderRows(streamReader, startDataRow);
        strLine = streamReader.ReadLine();
        CreateColumns(strLine);
        int rowCount = 0;
        while (null != (strLine = streamReader.ReadLine()))
        {
          if ("".Equals(strLine)) continue;
          String[] rowData = StringParser.ParseDelimitedString(strLine);
          if (1 == rowData.Length && rowData[0].Trim().ToUpper().Equals("**NOTE :")) break;
          List<String> rowDataList = rowData.ToList();
          rows.Add(rowDataList);
          rowCount++;
        }
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Loaded {0} rows from {1}", rows.Count, strPathFileName));
        return header;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString());
        return null;
      }
      finally
      {
        if (null != streamReader) streamReader.Close();
      }
    }
  }
}
