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 System.Reflection;

namespace Factories
{
    [ContentProperty("Decorators")]
    public partial class EntityView : UserControl
    {
        public EntityView()
        {
            InitializeComponent();

            MouseEnter += (s, e) =>
            {
                var parent = VisualTreeHelper.GetParent(this) as UIElement;
                if (parent != null) 
                    Canvas.SetZIndex(parent, 1);
            };
            MouseLeave += (s, e) =>
            {
                var parent = VisualTreeHelper.GetParent(this) as UIElement;
                if(parent != null)
                    Canvas.SetZIndex(parent, 0);
            };

            SetBinding(LatitudeProperty, new Binding("Latitude") { Mode=BindingMode.TwoWay });
            SetBinding(LongitudeProperty, new Binding("Longitude") { Mode = BindingMode.TwoWay });

            LayoutUpdated += (s, e) => UpdatePosition();
        }

        public string Icon 
        {
            set 
            {
                string asmName = new AssemblyName(Assembly.GetExecutingAssembly().FullName).Name;

                image.Source = new BitmapImage(new Uri(string.Format("/{0};component/Resources/{1}", asmName, value), UriKind.Relative)); }
        }

        public UIElement Decorator
        {
            set
            {
                decoratorGrid.Children.Clear();
                decoratorGrid.Children.Add(value);
            }
        }

        static readonly DependencyProperty LatitudeProperty =
           DependencyProperty.Register(
               "Latitude",
               typeof(double),
               typeof(EntityView),
               new PropertyMetadata(OnLatLongPropertyChanged));

        double Latitude
        {
            set { SetValue(LatitudeProperty, value); }
            get { return (double)GetValue(LatitudeProperty); }
        }
       
        static readonly DependencyProperty LongitudeProperty =
            DependencyProperty.Register(
                "Longitude",
                typeof(double),
                typeof(EntityView),
                new PropertyMetadata(OnLatLongPropertyChanged));

        double Longitude
        {
            set { SetValue(LongitudeProperty, value); }
            get { return (double)GetValue(LongitudeProperty); }
        }

        static void OnLatLongPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as EntityView).UpdatePosition();
        }

        void UpdatePosition()
        {
            C1MapCanvas.SetLatLong(this, new Point(Longitude, Latitude));            
        }
    }
}
