using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using System.Net;
using System.IO;
using System.Xml;
using Telerik.WebControls.Base.Calendar.Enumerations;

namespace MoonPhases
{
    public partial class _Default : System.Web.UI.Page
    {

        #region properties
        private int _phaseIndex;

        public List<string> Phases
        {
            get
            {
                List<string> phases = ViewState["Phases"] as List<string>;
                if (phases == null)
                {
                    _phaseIndex = 0;
                    phases = GetMoonPhases(RadCalendar1.FocusedDate.Date);
                }
                ViewState["Phases"] = phases;
                return phases;
            }
            set
            {
                ViewState["Phases"] = value;
            }
        }
        #endregion

        #region page event handlers
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadCalendar1.DefaultCellPadding = 5;
                RadCalendar1.DefaultCellSpacing = 5;
                RadCalendar1.AutoPostBack = true;
                RadCalendar1.MonthLayout = MonthLayout.Layout_7columns_x_6rows;
            }
        }
        #endregion

        #region private methods
        private List<string> GetMoonPhases(DateTime startDate)
        {
            List<string> phases = new List<string>();
            DateTime currentDate = startDate;
            for (int i = 0; i < RadCalendar1.SingleViewRows; i++)
            {
                LoadOneWeek(phases, currentDate);
                currentDate = currentDate.AddDays(RadCalendar1.SingleViewColumns);
            }
            return phases;
        }

        private void LoadOneWeek(List<string> phases, DateTime startDate)
        {
            const string urlFormat = @"http://www.trynt.com/moon-phase-api/v1/?d={0}&fo=xml&f=0";
            string url = string.Format(urlFormat, startDate.ToString("yyyy-MM-dd"));

            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(response.GetResponseStream());
                XmlTextReader xmlreader = new XmlTextReader(reader);
                if (xmlreader.ReadToFollowing("moon-phases"))
                {
                    // load one week worth of moon phase data
                    for (int i = 0; i < RadCalendar1.SingleViewColumns; i++)
                    {
                        if (xmlreader.ReadToFollowing("moon-phase-name"))
                        {
                            phases.Add(xmlreader.ReadString());
                        }
                    }
                }
            }
        }

        private string GetNextPhase()
        {
            string result = this.Phases[_phaseIndex];
            _phaseIndex += 1;
            return result;
        }

        #endregion

        #region calendar event handlers
        protected void RadCalendar1_DefaultViewChanged(object sender,
            Telerik.WebControls.Base.Calendar.Events.DefaultViewChangedEventArgs e)
        {
            this.Phases = null;
        }

        protected void RadCalendar1_DayRender(object sender,
            Telerik.WebControls.Base.Calendar.Events.DayRenderEventArgs e)
        {
            const string cellFormat = "<font style=\"color:{0}\"><b>{1}</b>&nbsp;&nbsp;{2}</font>";

            bool isInMonth = e.Day.Date.Month == RadCalendar1.FocusedDate.Month;
            string color = isInMonth ? "Black" : "Gray";
            e.Cell.Text = string.Format(cellFormat, color, e.Day.Date.Day, GetNextPhase());
        }
        #endregion

    }
}
