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.Xml.Linq;
using System.Globalization;
using System.Windows.Media.Imaging;

using Microsoft.VirtualEarth.MapControl;

namespace GeonamesClient
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            mapa.Children.Clear();
                     
            XDocument xml = XDocument.Parse(e.Result);

            var entries = from ent in xml.Descendants("entry")
                          select new WikipediaTopicInfo()
                          {
                              Lang = ent.Element("lang").Value,
                              Title = ent.Element("title").Value,
                              Summary = ent.Element("summary").Value,
                              Feature = ent.Element("feature").Value,
                              CountryCode = ent.Element("countryCode").Value,
                              Population = ent.Element("population").Value,
                              Elevation = ent.Element("elevation").Value,
                              Latitude = ent.Element("lat").Value,
                              Longitude = ent.Element("lng").Value,
                              WikipediaUrl = ent.Element("wikipediaUrl").Value,
                              ThumbnailImg = ent.Element("thumbnailImg").Value
                          };

            MapLayer pushpinsLayer = new MapLayer();

            foreach (var entry in entries)
            {
                Ellipse pushpin = new Ellipse();
                pushpin.Width = 10;
                pushpin.Height = 10;
                pushpin.Opacity = 0.7;
                pushpin.Tag = entry;
                pushpin.Fill = new SolidColorBrush(Colors.Red);

                pushpin.MouseMove += new MouseEventHandler(pushpin_MouseMove);                

                Location l = new Location();
                l.Latitude = Double.Parse(entry.Latitude, CultureInfo.InvariantCulture.NumberFormat);
                l.Longitude = Double.Parse(entry.Longitude, CultureInfo.InvariantCulture.NumberFormat);
                l.Altitude = Double.Parse(entry.Elevation, CultureInfo.InvariantCulture.NumberFormat);

                pushpinsLayer.AddChild(pushpin, l, PositionMethod.Center);
            }

            Dispatcher.BeginInvoke(() => { mapa.AddChild(pushpinsLayer); });
        }

        void pushpin_MouseMove(object sender, MouseEventArgs e)
        {
            WikipediaTopicInfo wti = (WikipediaTopicInfo)((FrameworkElement)sender).Tag;
            txtDescription.Text = wti.Title;
        }

        private void Map_MouseDragBox(object sender, MapMouseDragEventArgs e)
        {
            // Setaveni url pro volání služby GeoNames
            string url = String.Format("http://ws.geonames.org/wikipediaBoundingBox?north={0}&west={1}&south={2}&east={3}&lang=cs",
                                       mapa.ViewportPointToLocation(e.FromViewportPoint).Latitude.ToString(CultureInfo.InvariantCulture.NumberFormat),
                                       mapa.ViewportPointToLocation(e.FromViewportPoint).Longitude.ToString(CultureInfo.InvariantCulture.NumberFormat),
                                       mapa.ViewportPointToLocation(e.ViewportPoint).Latitude.ToString(CultureInfo.InvariantCulture.NumberFormat),
                                       mapa.ViewportPointToLocation(e.ViewportPoint).Longitude.ToString(CultureInfo.InvariantCulture.NumberFormat));

            // Asynchroní volání služby
            WebClient wb = new WebClient();
            wb.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wb_DownloadStringCompleted);
            wb.DownloadStringAsync(new Uri(url));
        }
    }
}
