using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Text;
using System.Windows.Threading;
using System.Xml.Linq;

namespace SilverlightGridWithTwitter
{
    public partial class Page : UserControl
    {
        int currentPageIndex = 1;
        int pageSize = 10;
        string urlFormat = "http://search.twitter.com/search.atom?q={0}&rpp={1}&page={2}";

        WebClient client = new WebClient();

        public Page()
        {
            InitializeComponent();
        }

        private void TextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (!String.IsNullOrEmpty(TextBox1.Text))
                {
                    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
                    
                    if (!client.IsBusy)
                        client.DownloadStringAsync(new Uri(String.Format(urlFormat, TextBox1.Text, pageSize, currentPageIndex)));
                }
                else
                {
                    RadGridView1.Visibility = Pager.Visibility = Visibility.Collapsed;
                }
            }
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            RadGridView1.Visibility = Pager.Visibility = Visibility.Visible;
            PageInfo.Text = String.Format("Page {0}", currentPageIndex);

            XNamespace atomNamespace = "http://www.w3.org/2005/Atom";

            RadGridView1.ItemsSource = from item in XDocument.Parse(e.Result).Descendants(atomNamespace + "entry")
                                       select new TwitterEntry
                                       {
                                           ID = item.Element(atomNamespace + "id").Value,
                                           Published = DateTime.Parse(item.Element(atomNamespace + "published").Value),
                                           Url = item.Element(atomNamespace + "link").Attribute("href").Value,
                                           Title = item.Element(atomNamespace + "title").Value,
                                           Content = item.Element(atomNamespace + "content").Value,
                                           Updated = DateTime.Parse(item.Element(atomNamespace + "updated").Value),
                                           ImageUrl = item.Descendants(atomNamespace + "link").Last().LastAttribute.Value,
                                           Author = new TwitterAuthor() 
                                           {
                                               Name = ((XElement) item.Element(atomNamespace + "author").FirstNode).Value,
                                               Url = ((XElement) item.Element(atomNamespace + "author").LastNode).Value
                                           }
                                       };

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (sender == Newer)
            {
                if (currentPageIndex >= 2)
                    currentPageIndex--;
            }
            else if (sender == Older)
            {
                currentPageIndex++;
            }

            if (currentPageIndex > 1)
            {
                Newer.Visibility = Visibility.Visible;
            }
            else
            {
                Newer.Visibility = Visibility.Collapsed;
            }

            if(!client.IsBusy)
                client.DownloadStringAsync(new Uri(String.Format(urlFormat, TextBox1.Text, pageSize, currentPageIndex)));
        }
    }

    public class TwitterEntry
    {
        public string ID { get; set; }
        public DateTime Published { get; set; }
        public string Url { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        public DateTime Updated { get; set; }
        public string ImageUrl { get; set; }
        public TwitterAuthor Author { get; set; }
    }

    public class TwitterAuthor
    {
        public string Name { get; set; }
        public string Url { get; set; }
    }
}