using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
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 C1.Silverlight.Maps;

namespace VectorLayer.Samples
{
  public partial class Cities : UserControl
  {
    C1VectorLayer vl;

    public Cities()
    {
        InitializeComponent();

        this.Loaded += Cities_Loaded;
        this.Unloaded += Cities_Unloaded;
    }

    void Cities_Unloaded(object sender, RoutedEventArgs e)
    {
        //Release memory
        if (vl != null)
        {
            foreach (var child in vl.Children)
            {
                var place = child as C1VectorPlacemark;
                if (place != null)
                {
                    place.Loaded -= C1VectorPlacemark_Loaded;
                }
            }
        }

        vl = null;
    }
    void Cities_Loaded(object sender, RoutedEventArgs e)
    {
        Color fc = Color.FromArgb(0xff, 0xC0, 0x50, 0x4d);
        maps.Foreground = new SolidColorBrush(fc);

        vl = new C1VectorLayer();

        Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("VectorLayer.Resources.Cities100K.txt");
        if (stream != null)
        {
            List<City> cities = City.Read(stream);
            vl.ItemTemplate = (DataTemplate)Resources["templCity"];
            IEnumerable<City> source = from city in cities orderby city.Population descending select city;
            vl.ItemsSource = source.Take(500);
        }
        maps.Layers.Add(vl);
    }

    private void C1VectorPlacemark_Loaded(object sender, RoutedEventArgs e)
    {
      C1VectorPlacemark pl = (C1VectorPlacemark)sender;
      City city = (City)pl.DataContext;
      ToolTipService.SetToolTip(pl, city.Name + ":\n" + city.Population.ToString());
    }
  }
}
