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.Media.Imaging;
using System.Diagnostics;
using System.Windows.Data;
using C1.Silverlight;
using System.Windows.Markup;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using C1.Silverlight.Maps;
using Olympics.WSData;

namespace Olympics
{
    public partial class MapCountryView 
        : UserControl
    {
        public MapCountryView()
        {
            InitializeComponent();
        }

        void MapCountryView_ZoomChanged(object sender, PropertyChangedEventArgs<double> e)
        {
            UpdateZoom(e.NewValue);
        }

        private const double ZoomLevelDetail = 3;

        private void UpdateZoom(double zoomValue)
        {
            if (zoomValue > ZoomLevelDetail)
            {
                LargeView.Visibility = Visibility.Visible;
                SmallView.Visibility = Visibility.Collapsed;
            }
            else
            {
                LargeView.Visibility = Visibility.Collapsed;
                SmallView.Visibility = Visibility.Visible;
            }
        }

        public CountryInfo Info 
        {
            get { return (CountryInfo)GetValue(InfoProperty); } 
            set { SetValue(InfoProperty, value); } 
        }

        static readonly DependencyProperty InfoProperty =
           DependencyProperty.Register(
               "Info",
               typeof(CountryInfo),
               typeof(MapCountryView),
               new PropertyMetadata(OnInfoPropertyChanged));
        

        static void OnInfoPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MapCountryView view = d as MapCountryView;
            if (view != null) 
            {
                view.OnInfoPropertyChanged();
            }
        }

        private void OnInfoPropertyChanged()
        {
            if (VisualTreeHelper.GetParent(this) != null)
            {
                C1MapCanvas.SetLatLong(VisualTreeHelper.GetParent(this), new Point(Info.Latitude, Info.Longitude));
            }

            if (!_attachedToZoom)
            {
                FindParentMaps().ZoomChanged += new EventHandler<PropertyChangedEventArgs<double>>(MapCountryView_ZoomChanged);
                _attachedToZoom = true;
                UpdateZoom(FindParentMaps().Zoom);
            }

            txtGold.Text = Info.GoldMedals.ToString();
            txtSilver.Text = Info.SilverMedals.ToString();
            txtBronze.Text = Info.BronzeMedals.ToString();
            txtName.Text = Info.CountryName;
            txtTotal.Text = Info.TotalMedals.ToString();
            ToolTipService.SetToolTip(SmallView, Info.CountryName);
        }

        bool _attachedToZoom;

        C1Maps FindParentMaps()
        {
            var e = VisualTreeHelper.GetParent(this) as FrameworkElement;
            while (VisualTreeHelper.GetParent(e) is FrameworkElement && !(e is C1Maps))
                e = VisualTreeHelper.GetParent(e) as FrameworkElement;
            return e as C1Maps;
        }
    }
}
