using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Compression;

namespace MarketData.CSVHelper
{
  public class ExtractFileHelper
  {
    public static List<String> GetExtractFiles(String extractFolder)
    {
      try
      {
        List<String> fileList = new List<String>();
        String[] arFileList = Directory.GetFiles(extractFolder, "*.csv");
        for (int index = 0; index < arFileList.Length; index++) fileList.Add(arFileList[index]);
        return fileList;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception: {0}",exception.ToString()));
        return null;
      }
    }
    public static List<String> ExtractToFolder(String strPathFileName, String extractToFolder)
    {
      List<String> strPathExtractedFiles = new List<String>();
      try
      {
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Extracting contents of {0} to {1}", strPathFileName, extractToFolder));
        using (ZipArchive archive = ZipFile.OpenRead(strPathFileName))
        {
          foreach (ZipArchiveEntry entry in archive.Entries)
          {
            MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Extracting {0}", entry.FullName));
            String strPathFullName = extractToFolder + "/" + entry.FullName;
            String strDirectoryName = null;
            if (entry.FullName.Contains('\\') || entry.FullName.Contains('/')) strDirectoryName = Path.GetDirectoryName(entry.FullName);
            if (null != strDirectoryName)
            {
              if (!Directory.Exists(extractToFolder + "/" + strDirectoryName))
              {
                Directory.CreateDirectory(extractToFolder + "/" + strDirectoryName);
              }
            }
            if (File.Exists(strPathFullName))
            {
              try { File.Delete(strPathFullName); }
              catch (Exception exception)
              {
                MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception removing file {0}, exception was {1}, continuing with extraction.", strPathFullName, exception.Message));
              }
            }
            try { entry.ExtractToFile(strPathFullName, true); }
            catch (Exception exception)
            {
              MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Unable to extract the file {0} from {1}, exception was {2}", strPathFullName, strPathFileName, exception.Message));
              continue;
            }
            strPathExtractedFiles.Add(strPathFullName);
          }
        }
        return strPathExtractedFiles;
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString());
        return strPathExtractedFiles;
      }
    }
    public static void RemoveZipFile(String strPathFileName)
    {
      try
      {
        File.Delete(strPathFileName);
      }
      catch (Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString());
      }
    }
  }
}
