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.ComponentModel;

namespace SLCommon.MapElements.Pushpins
{
    public partial class ucScalablePushpin : UserControl
    {
        #region Properties

        //public double PinSize { get; set; }
        //public SolidColorBrush PinColor { get; set; }

        public double PinStrokeThickness { get; set; }
        public SolidColorBrush PinStrokeColor { get; set; }

        #endregion

        #region Constructors

        public ucScalablePushpin()
        {
            InitializeComponent();
        }

        #endregion

        #region Dependency properties

        public double PinSize
        {
            get
            {
                return (double)GetValue(PinSizeProperty);
            }
            set
            {
                SetValue(PinSizeProperty, value);
            }
        }

        public static readonly DependencyProperty PinSizeProperty =
            DependencyProperty.Register(
                "PinSize",
                typeof(double),
                typeof(ucScalablePushpin),
                new PropertyMetadata((double)10, new PropertyChangedCallback(OnChangeSize)
            )
        );

        private static void OnChangeSize(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as ucScalablePushpin).changeSize(Convert.ToDouble(e.NewValue));
        }

        private void changeSize(double size)
        {
            double minSize = 5;
            if (size > minSize)
            {
                this.pin.Height = size;
                this.pin.Width = size;
            }
            else
            {
                this.pin.Height = minSize;
                this.pin.Width = minSize;
            }

            this.pin.RenderTransform = new CompositeTransform()
            {
                TranslateX = -this.pin.Width / 2,
                TranslateY = -this.pin.Height / 2
            };
        }


        public SolidColorBrush PinFillColor
        {
            get
            {
                return (SolidColorBrush)GetValue(PinFillColorProperty);
            }
            set
            {
                SetValue(PinFillColorProperty, value);
            }
        }

        public static readonly DependencyProperty PinFillColorProperty =
            DependencyProperty.Register(
                "PinFillColor",
                typeof(SolidColorBrush),
                typeof(ucScalablePushpin),
                new PropertyMetadata(
                    (SolidColorBrush)new SolidColorBrush(Colors.Red), 
                    new PropertyChangedCallback(OnChangeFillColor)
                )
            );

        private static void OnChangeFillColor(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            (source as ucScalablePushpin).changeColor((SolidColorBrush)e.NewValue);
        }

        private void changeColor(SolidColorBrush colorBrush)
        {
            this.pin.Fill = colorBrush;
        }

        #endregion
    }
}
