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

namespace MarketData.Utils
{
  public class UpdateManager
  {
    private StreamWriter streamWriter=null;
    private FileStream fileStream=null;
    private Dictionary<String,String> entries=new Dictionary<String,String>();

    public UpdateManager()
    {
    }
    public bool Prepare(String strPathFileName, int maxAgeDays=5)
    {
      try
      {
        String currentWorkingDirectory=Directory.GetCurrentDirectory();
        if(!File.Exists(strPathFileName)||IsExpired(strPathFileName, maxAgeDays))
        {
          if(File.Exists(strPathFileName))File.Delete(strPathFileName);
          fileStream=new FileStream(strPathFileName,FileMode.Create);
          streamWriter=new StreamWriter(fileStream);
          MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Creating session file:{0}",strPathFileName));
        }
        else
        {
          FileStream fileStream=new FileStream(strPathFileName,FileMode.Open,FileAccess.ReadWrite,FileShare.Read);
          StreamReader streamReader=new StreamReader(fileStream);
          String strLine=null;
          while(null!=(strLine=streamReader.ReadLine()))
          {
            if(!entries.ContainsKey(strLine))entries.Add(strLine,strLine);
          }
          MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Loaded {0} entries from session file:{1}",entries.Count,strPathFileName));
          streamReader.Close();
          streamReader.Dispose();
          fileStream.Close();
          fileStream.Dispose();
          fileStream=new FileStream(strPathFileName,FileMode.Append);
          streamWriter=new StreamWriter(fileStream);
        }
        return true;
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception));
        return false;
      }
    }
    private bool IsExpired(String strPathFileName, int maxAgeDays)
    {
      try
      {
        DateGenerator dateGenerator=new DateGenerator();
        DateTime creationTime=File.GetCreationTime(strPathFileName);
        int daysElapsed=Math.Abs(dateGenerator.DaysBetweenActual(creationTime,DateTime.Now));
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Ok. {0} is {1} days(s) old. MaxAge:{2} days",strPathFileName,daysElapsed,maxAgeDays));
        if(daysElapsed>maxAgeDays)
        {
          MDTrace.WriteLine(LogLevel.DEBUG,String.Format("{0} is expired. {1} days old.  MaxAge:{2} days",strPathFileName,daysElapsed,maxAgeDays));
          return true;
        }
        return false;
      }
      catch(Exception exception)
      {
        MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception));
        return true;
      }
    }
    public List<String> Entries
    {
      get{return new List<String>(entries.Keys);}
    }
    public void Add(String entry)
    {
      lock(this)
      {
        if(null==streamWriter)return;
        streamWriter.WriteLine(entry);
        streamWriter.Flush();
      }
    }
    public void Dispose()
    {
      if(null!=streamWriter){streamWriter.Close();streamWriter.Dispose();}
      if(null!=fileStream){fileStream.Close();fileStream.Dispose();}
    }
  }
}

