using System;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;

public partial class Reports_GetWeekMonthDate : Base.BasePage
{
    #region Global Variable Declaration
    int intType;
    ResourceManager rm;
    CultureInfo ci;
    string ConcatenateString = string.Empty;
    int delta = 0;
    cls_Common objCommon = new cls_Common();
    int intMonthStartDay, intMonthEndDay;
    int days;
    #endregion
    #region Page Load
    protected override void Page_Load(object sender, EventArgs e)
    {
        GC.Collect();
        base.Page_Load(sender, e);
        string source_Language;
        source_Language = Convert.ToString(Session["Session_ActiveLanguage"]);
        rm = new ResourceManager(source_Language, System.Reflection.Assembly.Load("App_GlobalResources"));
        ci = Thread.CurrentThread.CurrentCulture;
        //Get Type of Date Filter
        if (Request.QueryString["Type"] != null && Request.QueryString["Type"] != "")
        {
            intType = Convert.ToInt32(Request.QueryString["Type"].ToString());
        }
        intMonthStartDay = Convert.ToInt32(Session["MonthStartDay"]);
        intMonthEndDay = Convert.ToInt32(Session["MonthEndDay"]);
        GetDate();
        Response.Write(ConcatenateString);
        Response.End();
    }
    #endregion
    #region User Defined Function
    private void GetDate()
    {
        DateTime input;
        DateTime nowdate = DateTime.Now;
        ConcatenateString = string.Empty;
        input = DateTime.Now;
        //Get Week Start Date And Week End Date
        if (intType == 1)
        {
            input = input.AddDays(-7);
        }
        if (intType == 1 || intType == 0)
        {
            DateTime WeekEndDay = input;
            DateTime WeekStartDay = input;
            switch (Convert.ToString(Session["WeekStartDay"]))
            {
                case "1":
                    while (WeekStartDay.DayOfWeek != DayOfWeek.Sunday)
                        WeekStartDay = WeekStartDay.AddDays(-1);
                    break;
                case "2":
                    while (WeekStartDay.DayOfWeek != DayOfWeek.Monday)
                        WeekStartDay = WeekStartDay.AddDays(-1);
                    break;
                case "3":
                    while (WeekStartDay.DayOfWeek != DayOfWeek.Tuesday)
                        WeekStartDay = WeekStartDay.AddDays(-1);
                    break;
                case "4":
                    while (WeekStartDay.DayOfWeek != DayOfWeek.Wednesday)
                        WeekStartDay = WeekStartDay.AddDays(-1);
                    break;
                case "5":
                    while (WeekStartDay.DayOfWeek != DayOfWeek.Thursday)
                        WeekStartDay = WeekStartDay.AddDays(-1);
                    break;
                case "6":
                    while (WeekStartDay.DayOfWeek != DayOfWeek.Friday)
                        WeekStartDay = WeekStartDay.AddDays(-1);
                    break;
                case "7":
                    while (WeekStartDay.DayOfWeek != DayOfWeek.Saturday)
                        WeekStartDay = WeekStartDay.AddDays(-1);
                    break;

            }
            ConcatenateString = (Convert.ToDateTime(WeekStartDay)).ToString(Convert.ToString(Session["MDateFormat"]));
            WeekEndDay = WeekStartDay;
            switch (Convert.ToString(Session["WeekEndDay"]))
            {
                case "1":
                    while (WeekEndDay.DayOfWeek != DayOfWeek.Sunday)
                        WeekEndDay = WeekEndDay.AddDays(1);
                    break;
                case "2":
                    while (WeekEndDay.DayOfWeek != DayOfWeek.Monday)
                        WeekEndDay = WeekEndDay.AddDays(1);
                    break;
                case "3":
                    while (WeekEndDay.DayOfWeek != DayOfWeek.Tuesday)
                        WeekEndDay = WeekEndDay.AddDays(1);
                    break;
                case "4":
                    while (WeekEndDay.DayOfWeek != DayOfWeek.Wednesday)
                        WeekEndDay = WeekEndDay.AddDays(1);
                    break;
                case "5":
                    while (WeekEndDay.DayOfWeek != DayOfWeek.Thursday)
                        WeekEndDay = WeekEndDay.AddDays(1);
                    break;
                case "6":
                    while (WeekEndDay.DayOfWeek != DayOfWeek.Friday)
                        WeekEndDay = WeekEndDay.AddDays(1);
                    break;
                case "7":
                    while (WeekEndDay.DayOfWeek != DayOfWeek.Saturday)
                        WeekEndDay = WeekEndDay.AddDays(1);
                    break;
            }
            if (nowdate < WeekEndDay)
            {
                ConcatenateString += "$" + (Convert.ToDateTime(nowdate)).ToString(Convert.ToString(Session["MDateFormat"]));
            }
            else
            {
                ConcatenateString += "$" + (Convert.ToDateTime(WeekEndDay)).ToString(Convert.ToString(Session["MDateFormat"]));
            }
        }
        //End Get Week Start Date And End Date
        //Get Custom Start Date And End Date
        else if (intType == 3)
        {
            ConcatenateString = (Convert.ToDateTime(objCommon.CurrentMonthStartDate())).ToString(Convert.ToString(Session["MDateFormat"]));
            ConcatenateString += "$" + (Convert.ToDateTime(System.DateTime.Now.ToShortDateString())).ToString(Convert.ToString(Session["MDateFormat"]));

        }
        //End Get Custom Start Date And End Date
        //Get This Month Start Date And End Date
        else if (intType == 4 || intType == 5)
        {
            DateTime curDate;
            if (intType == 4)
                curDate = DateTime.Now;
            else
                curDate = DateTime.Now.AddMonths(-1);
            int TodayDay = 0;
            TodayDay = curDate.Day;
            int days = DateTime.DaysInMonth(curDate.Year, curDate.Month);
            if (days < intMonthStartDay)
            {
                intMonthStartDay = days;
            }
            if (days < intMonthEndDay)
            {
                intMonthEndDay = days;
            }
            DateTime StartDate = new DateTime(curDate.Year, curDate.Month, intMonthStartDay);
            DateTime EndDate = new DateTime(curDate.Year, curDate.Month, intMonthEndDay);
            if (curDate < StartDate)
            {
                StartDate = StartDate.AddMonths(-1);
            }
            else
            {
                StartDate = StartDate;
            }

            if (curDate < EndDate)
            {
                EndDate = EndDate;
            }
            else if (curDate > EndDate)
            {
                if (StartDate > EndDate)
                {
                    int days1 = DateTime.DaysInMonth(curDate.Year, curDate.Month + 1);
                    if (days1 < intMonthEndDay)
                    {
                        intMonthEndDay = days1;
                    }
                    EndDate = new DateTime(curDate.Year, curDate.Month + 1, intMonthEndDay);
                }
                else
                {
                    int days2 = DateTime.DaysInMonth(curDate.Year, curDate.Month);
                    if (days2 < intMonthEndDay)
                    {
                        intMonthEndDay = days2;
                    }
                    EndDate = new DateTime(curDate.Year, curDate.Month, intMonthEndDay);
                    //StartDate = new DateTime(curDate.Year, curDate.Month, Convert.ToInt32(Session["MonthStartDay"]));
                }
            }
            else
            {
                EndDate = EndDate.AddMonths(1);
            }
            ConcatenateString = (Convert.ToDateTime(StartDate)).ToString(Convert.ToString(Session["MDateFormat"]));
            if (nowdate < EndDate)
            {
                ConcatenateString += "$" + (Convert.ToDateTime(nowdate)).ToString(Convert.ToString(Session["MDateFormat"]));
            }
            else
            {
                ConcatenateString += "$" + (Convert.ToDateTime(EndDate)).ToString(Convert.ToString(Session["MDateFormat"]));
            }

        }
        //End Get This Month Start Date And End Date
        //Get Previous Month Start Date And End Date
        else if (intType == 5)
        {
            DateTime curDate = DateTime.Now;
            curDate = curDate.AddMonths(-1);
            int TodayDay = 0;
            TodayDay = curDate.Day;
            days = DateTime.DaysInMonth(curDate.Year, curDate.Month);
            if (days < intMonthStartDay)
            {
                intMonthStartDay = days;
            }
            if (days < intMonthEndDay)
            {
                intMonthEndDay = days;
            }
            DateTime StartDate = new DateTime(curDate.Year, curDate.Month, intMonthStartDay);
            DateTime EndDate = new DateTime(curDate.Year, curDate.Month, intMonthEndDay);
            if (curDate < StartDate)
            {
                StartDate = StartDate.AddMonths(-1);
            }
            else
            {
                StartDate = StartDate;
            }

            if (curDate < EndDate)
            {
                EndDate = EndDate;
            }
            else if (curDate > EndDate)
            {
                if (StartDate > EndDate)
                {
                    days = DateTime.DaysInMonth(curDate.Year, curDate.Month + 1);
                    if (days < intMonthEndDay)
                    {
                        intMonthEndDay = days;
                    }
                    EndDate = new DateTime(curDate.Year, curDate.Month + 1, intMonthEndDay);
                }
                else
                {
                    days = DateTime.DaysInMonth(curDate.Year, curDate.Month);
                    if (days < intMonthEndDay)
                    {
                        intMonthEndDay = days;
                    }
                    days = DateTime.DaysInMonth(curDate.Year, curDate.Month - 1);
                    if (days < intMonthStartDay)
                    {
                        intMonthStartDay = days;
                    }
                    EndDate = new DateTime(curDate.Year, curDate.Month, intMonthEndDay);
                    StartDate = new DateTime(curDate.Year, curDate.Month - 1, intMonthStartDay);
                }
            }
            else
            {
                EndDate = EndDate.AddMonths(1);
            }
            ConcatenateString = (Convert.ToDateTime(StartDate)).ToString(Convert.ToString(Session["MDateFormat"]));
            ConcatenateString += "$" + (Convert.ToDateTime(EndDate)).ToString(Convert.ToString(Session["MDateFormat"]));
        }
        //End Get Previous Month Start Date And End Date
    }
    #endregion
}
