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 C1.Silverlight;
using System.Windows.Controls.Primitives;

namespace Factories
{
    public partial class ToolbarItem : UserControl
    {
        Uri _iconUri, _iconDisable;
        UIElement _target;
        Popup _dragIcon;
        C1DragHelper _dragHelper;
        Point grabPoint = new Point();
        bool _isEnabled = true, _isMouseOver;
    
        public ToolbarItem()
        {
            InitializeComponent();

            MouseEnter += (s,e) => 
            {
                _isMouseOver = true;
                UpdateVisualState();
            };

            MouseLeave += (s, e) =>
            {
                _isMouseOver = false;
                UpdateVisualState();
            };

            _dragIcon = new Popup();
            _dragHelper = new C1DragHelper(this);

            _dragHelper.DragStarted += (s, e) =>
            {
                if (!IsEnabled)
                    return;
                
                _dragIcon.Child = new Image { Source = new BitmapImage(Icon) };
                _dragIcon.IsOpen = true;

                grabPoint = Application.Current.RootVisual.TransformToVisual(this).Transform(e.Origin);

                _isMouseOver = false;
                UpdateVisualState();
            };

            _dragHelper.DragDelta += (s, e) =>
            {
                if (!IsEnabled)
                    return;

                var position = e.GetPosition(null);
                _dragIcon.HorizontalOffset = position.X - grabPoint.X;
                _dragIcon.VerticalOffset = position.Y - grabPoint.Y;
            };

            _dragHelper.DragCompleted += (s, e) =>
            {
                if (!IsEnabled)
                    return;

                if (_target != null && DropDelegate != null)
                {
                    var target = e.GetPosition(_target);
                    var child = _dragIcon.Child as FrameworkElement;
                    DropDelegate(new Point(
                        target.X - grabPoint.X + child.ActualWidth / 2,
                        target.Y - grabPoint.Y + child.ActualHeight / 2));
                }

                _dragIcon.IsOpen = false;
            };
        }

        public Uri Icon 
        {
            set 
            { 
                _iconUri = value;
                UpdateVisualState();
            }
            get { return _iconUri; }
        }

        public Uri DisableIcon
        {
            set
            {
                _iconDisable = value;
                UpdateVisualState();
            }
            get { return _iconDisable; }
        }

        public string Tooltip
        {
            get { return ToolTipService.GetToolTip(this) as string; }
            set { ToolTipService.SetToolTip(this, value); }
        }

        public UIElement Target
        {
            set 
            {
                _target = value;
            }
            get { return _target; }
        }

        public Action<Point> DropDelegate { get; set; }

        public bool IsEnabled
        {
            get { return _isEnabled; }
            set
            {
                _isEnabled = value;
                Cursor = IsEnabled ? Cursors.Hand : Cursors.Arrow;
                UpdateVisualState();
            }
        }

        public string Label
        {
            get { return label.Text; }
            set { label.Text = value; }
        }

        void UpdateVisualState()
        {
            if (_isEnabled)
            {
                if (_isMouseOver)
                {
                    mouseOver.Begin();
                    mouseOut.Stop();
                }
                else
                {
                    mouseOut.Begin();
                    mouseOver.Stop();
                }

                if(Icon != null)
                    image.Source = new BitmapImage(Icon); 
            }
            else 
            {
                if (DisableIcon != null)
                    image.Source = new BitmapImage(DisableIcon);
                mouseOut.Stop();
                mouseOver.Stop();
            }
        }
    }
}
