using System;
using System.Text;
using System.Web.UI;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                C1InputDate1.Date = DateTime.Now;
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string desde = "", hasta = "", dateA = "", dateB = "", dateNow = "";
            string queryDateHasta = "";
            string txtTreeColsValue = ddlMode.SelectedValue;

            DateTime d1;
            DateTime d2 = DateTime.Now;

            if (C1InputDate1.Date.HasValue)
            {
                d1 = C1InputDate1.Date.Value;
            }
            else
            {
                d1 = new DateTime(DateTime.Now.Year, 1, 1);
            }

            string year = d1.Year.ToString();
            string month = d1.Month.ToString("00");
            string day = d1.Day.ToString("00");

            desde = $"{year}/{month}/{day} 00:00";
            dateA = $"{day}/{month}/{year} 00:00";

            switch (txtTreeColsValue)
            {
                case "TS1":
                    #region hourly
                    if (d1.Date == d2.Date)
                    {
                        hasta = $"{year}/{month}/{day} {d2.Hour:00}:{d2.Minute:00}:{d2.Second:00}";
                        queryDateHasta = $"{day}/{month}/{year} {d2.Hour:00}:{d2.Minute:00}:{d2.Second:00}";
                        dateB = queryDateHasta;
                    }
                    else
                    {
                        hasta = $"{year}/{month}/{day} 23:59";
                        queryDateHasta = $"{day}/{month}/{year} 23:59";
                        dateB = queryDateHasta;
                    }
                    dateNow = d2.ToShortDateString();
                    break;
                #endregion

                case "TS2":
                    #region monthly
                    if (d1.Month == d2.Month && d1.Year == d2.Year)
                    {
                        hasta = $"{year}/{month}/{d2.Day:00} {d2.Hour:00}:{d2.Minute:00}:{d2.Second:00}";
                        queryDateHasta = $"{month}/{year} {d2.Hour:00}:{d2.Minute:00}:{d2.Second:00}";
                        dateB = $"{d2.Day:00}/{month}/{year} {d2.Hour:00}:{d2.Minute:00}:{d2.Second:00}";
                    }
                    else
                    {
                        int lastDay = GetMonthDays(d1.Month - 1, d1.Year);
                        desde = $"{year}/{month}/01 00:00:00";
                        hasta = $"{year}/{month}/{lastDay:00} 23:59";
                        queryDateHasta = $"{month}/{year} 23:59";
                        dateB = $"{lastDay:00}/{month}/{year} 23:59";
                    }
                    break;
                #endregion

                default:
                    #region annually
                    if (d1.Year == d2.Year)
                    {
                        desde = $"{year}/01/01 00:00:00";
                        hasta = $"{year}/{d2.Month:00}/{d2.Day:00} {d2.Hour:00}:{d2.Minute:00}:{d2.Second:00}";
                        dateB = $"{d2.Day:00}/{d2.Month:00}/{year} {d2.Hour:00}:{d2.Minute:00}:{d2.Second:00}";
                    }
                    else
                    {
                        desde = $"{year}/01/01 00:00:00";
                        hasta = $"{year}/12/31 23:59";
                        dateB = $"31/12/{year} 23:59";
                    }
                    break;
                    #endregion
            }

            var sb = new StringBuilder();
            void Row(string k, string v) =>
                sb.Append($"<div class='result-row'><span class='key'>{k}</span><span class='val'>{v}</span></div>");

            Row("Mode", txtTreeColsValue);
            Row("d1", d1.ToString("dd/MM/yyyy"));
            Row("desde", desde);
            Row("hasta", hasta);
            Row("dateA", dateA);
            Row("dateB", dateB);
            Row("queryDateHasta", queryDateHasta);
            Row("dateNow", dateNow);

            ResultLabel.Text = sb.ToString();
            resultBox.Visible = true;
        }

        private int GetMonthDays(int month, int year)
        {
            return DateTime.DaysInMonth(year, month + 1);
        }
    }
}