using C1.Xaml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace ControlExplorer
{
    public partial class TileListBoxSample : UserControl
    {
        public TileListBoxSample()
        {
            InitializeComponent();

            Loaded += OnLoaded;
        }

        void OnLoaded(object sender, RoutedEventArgs e)
        {
            LoadVideos();
        }

        private async void LoadVideos()
        {
            try
            {
                loading.Visibility = Visibility.Visible;
                listBox.Visibility = Visibility.Collapsed;
                retry.Visibility = Visibility.Collapsed;
                var feed = new YoutubeFeed();
                try
                {
                    var videos = await feed.LoadMoreItemsAsync(48);
                    listBox.ItemsSource = feed;
                    listBox.Visibility = Visibility.Visible;
                    retry.Visibility = Visibility.Collapsed;
                }
                catch
                {
                    var dialog = new MessageDialog("There was an error when attempting to download data from you tube.");
                    dialog.ShowAsync();
                    retry.Visibility = Visibility.Visible;
                }
                finally
                {
                    loading.Visibility = Visibility.Collapsed;
                }
            }
            catch
            {
                var dialog = new MessageDialog("There was an error when attempting to download data from you tube.");
                dialog.ShowAsync();
                loading.Visibility = Visibility.Collapsed;
                retry.Visibility = Visibility.Visible;
            }
        }

        private void listBox_ItemTapped(object sender, C1TappedEventArgs e)
        {
            var video = (sender as C1ListBoxItem).Content as Video;
            NavigateUrl(video.Link);
            e.Handled = true;
        }

        public async Task NavigateUrl(string url)
        {
            try
            {
                await Launcher.LaunchUriAsync(new Uri(url));
            }
            catch { }
        }

        private void Retry_Click(object sender, RoutedEventArgs e)
        {
            LoadVideos();
        }
    }

    public class YoutubeFeed : IncrementalLoadingBase
    {
        private bool _hasMoreItems = true;
        private uint _lastIndex = 0;
        protected override async Task<IList<object>> LoadMoreItemsOverrideAsync(CancellationToken c, uint count)
        {
            var videos = await LoadVideosAsync(_lastIndex + 1, Math.Min(count, 500 - _lastIndex));
            _lastIndex += (uint)videos.Count;
            _hasMoreItems = _lastIndex < 500;
            return videos.Cast<object>().ToList();
        }

        protected override bool HasMoreItemsOverride()
        {
            return _hasMoreItems;
        }

        private async Task<List<Video>> LoadVideosAsync(uint startIndex, uint maxResults)
        {
            var youtubeUrl = string.Format("https://gdata.youtube.com/feeds/api/videos?q=windows+8&start-index={0}&max-results={1}", startIndex, maxResults);
            var AtomNS = "http://www.w3.org/2005/Atom";
            var MediaNS = "http://search.yahoo.com/mrss/";

            var videos = new List<Video>();
            var client = WebRequest.CreateHttp(new Uri(youtubeUrl));
            try
            {
                var response = await client.GetResponseAsync();

                #region ** parse you tube data
                var doc = XDocument.Load(response.GetResponseStream());
                foreach (var entry in doc.Descendants(XName.Get("entry", AtomNS)))
                {
                    var title = entry.Element(XName.Get("title", AtomNS)).Value;
                    var thumbnail = "";
                    var group = entry.Element(XName.Get("group", MediaNS));
                    var thumbnails = group.Elements(XName.Get("thumbnail", MediaNS));
                    var thumbnailElem = thumbnails.FirstOrDefault();
                    if (thumbnailElem != null)
                        thumbnail = thumbnailElem.Attribute("url").Value;
                    var alternate = entry.Elements(XName.Get("link", AtomNS)).Where(elem => elem.Attribute("rel").Value == "alternate").FirstOrDefault();
                    var link = alternate.Attribute("href").Value;
                    videos.Add(new Video() { Title = title, Link = link, Thumbnail = thumbnail });
                }
                #endregion
            }
            catch (WebException exc)
            {
                var excText = new StreamReader(exc.Response.GetResponseStream()).ReadToEnd();
                throw;
            }

            return videos;

        }

    }

    public class Video
    {
        public string Title { get; set; }
        public string Thumbnail { get; set; }
        public string Link { get; set; }
    }
}
