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.Windows.Data;
using System.Xml.Linq;
using C1.Silverlight;
using System.IO;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Toolkit.DataSources;
using System.ServiceModel.Syndication;
using ESRI.ArcGIS.Client.Tasks;

namespace EsriMapSamples
{
    [System.ComponentModel.Description("This sample demonstrates the GeoRSSLayer which makes it easy to display GeoRSS-Simple feeds as graphics on a map.")]
    public partial class FlickerGeoRSS : UserControl
    {
        private static ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();

        public FlickerGeoRSS()
        {
            InitializeComponent();

            GeoRssLayer rssLayer = map1.Layers["GeoRSSLayer"] as GeoRssLayer;
            rssLayer.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(rssLayer_PropertyChanged);
        }

        void rssLayer_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "FullExtent")
            {
                GeoRssLayer rssLayer = map1.Layers["GeoRSSLayer"] as GeoRssLayer;
                map1.ZoomTo(rssLayer.FullExtent);
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(tb.Text))
            {
                tb.IsEnabled = false;
                btnLoad.IsEnabled = false;
                string source = string.Format("http://api.flickr.com/services/feeds/geo/{0}", tb.Text);
                
                GeoRssLayer rssLayer = map1.Layers["GeoRSSLayer"] as GeoRssLayer;
                rssLayer.ClearGraphics();
                rssLayer.Source = new Uri(source);
                rssLayer.Update();

                tb.IsEnabled = true;
                btnLoad.IsEnabled = true;
            }
        }

        private void tb_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                Button_Click(btnLoad, new RoutedEventArgs());
        }
    }

    /// <summary>
    /// Parses image url out of SyndicationItem object
    /// </summary>
    public class ImageConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //if you want to display images in the MapTip uncomment the code below
            //if (targetType.Equals(typeof(ESRI.ArcGIS.Client.DataBinding)))
            //{
                var v = value as ESRI.ArcGIS.Client.DataBinding;
                if (v != null)
                {
                    var dict = v.Attributes as IDictionary<string, object>;

                    SyndicationItem feed = dict["FeedItem"] as SyndicationItem;
                    if (feed != null)
                    {
                        TextSyndicationContent tsc = feed.Content as TextSyndicationContent;
                        if (tsc != null)
                        {
                            string imagePath = tsc.Text;
                            imagePath = imagePath.Substring(imagePath.IndexOf("src="));
                            imagePath = imagePath.Substring(imagePath.IndexOf("\"") + 1);
                            return imagePath.Substring(0, imagePath.IndexOf("\""));
                        }

                    }
                }
            //}
            //else
            //{
            //    dict = value as IDictionary<string, object>;
            //}
              
            return null;
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value;
        }
    }
}
