using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MarketData.Utils;
using System.Collections.ObjectModel;

namespace MarketData.Helper
{
	public class TimeZoneHelper
	{
		private DateGenerator dateGenerator=new DateGenerator();
		private DateTime startDate;
		private DateTime endDate;
		private DateTime prevBusinessDate;
		private DateTime easternTime;
		private TimeZoneInfo localZone = TimeZoneInfo.Local;
		private TimeZone currentTimeZone=TimeZone.CurrentTimeZone;
    private static TimeZoneInfo easternTimeZone = GetEasternTimeZone();

		public TimeZoneHelper()
		{
			startDate=DateTime.Now;
			endDate = startDate;      // the most recent date
			prevBusinessDate=startDate;
			prevBusinessDate = dateGenerator.GetPrevBusinessDay(endDate);
			easternTime=new DateTime(prevBusinessDate.Year,prevBusinessDate.Month,prevBusinessDate.Day,20,30,0);

			if(!currentTimeZone.DaylightName.Equals("Eastern Daylight Time")&&!currentTimeZone.DaylightName.Equals("Eastern Time (US & Canada)"))
			{
				easternTime=TimeZoneInfo.ConvertTimeToUtc(new DateTime(easternTime.Year,easternTime.Month,easternTime.Day,20,30,0),easternTimeZone);
			}
			if(prevBusinessDate.Equals(endDate)&&DateTime.Now.TimeOfDay<new TimeSpan(easternTime.Hour,easternTime.Minute,easternTime.Second))
			{
				prevBusinessDate=dateGenerator.FindPrevBusinessDay(prevBusinessDate);
			}
		}
		public TimeZoneHelper(DateTime startDate)
		{
			this.startDate=startDate;
			endDate = startDate;      // the most recent date
			prevBusinessDate=startDate;
			prevBusinessDate = dateGenerator.GetPrevBusinessDay(endDate);
			easternTime=new DateTime(prevBusinessDate.Year,prevBusinessDate.Month,prevBusinessDate.Day,20,30,0);
			if(!currentTimeZone.DaylightName.Equals("Eastern Daylight Time")&&!currentTimeZone.DaylightName.Equals("Eastern Time (US & Canada)"))
			{
        easternTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(easternTime.Year, easternTime.Month, easternTime.Day, 20, 30, 0), easternTimeZone);
			}
			if(prevBusinessDate.Equals(endDate)&&DateTime.Now.TimeOfDay<new TimeSpan(easternTime.Hour,easternTime.Minute,easternTime.Second))
			{                                               
				prevBusinessDate=dateGenerator.FindPrevBusinessDay(prevBusinessDate);
			}
		}
    private static TimeZoneInfo GetEasternTimeZone()
    {
      TimeZoneInfo timeZoneInfo = null;
      ReadOnlyCollection<TimeZoneInfo> timeZones;
      timeZones= TimeZoneInfo.GetSystemTimeZones();
      for (int index = 0; index < timeZones.Count; index++)
      {
        timeZoneInfo = timeZones[index];
        if (timeZoneInfo.DisplayName.Equals("Eastern Daylight Time")||timeZoneInfo.DisplayName.Equals("Eastern Time (US & Canada)")||
            timeZoneInfo.DaylightName.Equals("Eastern Daylight Time")||timeZoneInfo.StandardName.Equals("Eastern Standard Time")) break;
      }
      return timeZoneInfo;
    }
		public DateTime StartDate
		{
			get{return startDate;}
		}
		public DateTime EndDate
		{
			get{return endDate;}
		}
		public DateTime PrevBusinessDate
		{
			get{return prevBusinessDate;}
		}
		public DateTime EasternTime
		{
			get{return easternTime;}
		}
		public DateGenerator DateGenerator
		{
			get{return dateGenerator;}
		}
	}
}
