using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls.DragDrop;
using Telerik.Windows.Controls;

namespace SilverlightApplication9
{
    public class TransparentDragCue
    {
        private static SolidColorBrush transparentBrush = new SolidColorBrush(Colors.Transparent);

        private static bool isEnabled;

        public static bool IsEnabled
        {
            get { return isEnabled; }
            set
            {
                if (isEnabled != value && value)
                {
                    isEnabled = value;

                    RegisterDragInfoHandler();
                }

            }
        }

        private static void RegisterDragInfoHandler()
        {
            EventManager.RegisterClassHandler(typeof(FrameworkElement), RadDragAndDropManager.DragInfoEvent, new EventHandler<DragDropEventArgs>(OnDragInfo));
        }


        public static void OnDragInfo(object sender, DragDropEventArgs args)
        {
            if (!backgroundApplied && args.Options.DragCue != null && IsEnabled && ((FrameworkElement)args.Options.DragCue).Parent != null)
            {
                var dragCueParent = ((FrameworkElement)args.Options.DragCue).Parent as FrameworkElement;

                if (dragCueParent != null)
                {
                    var wrapperGrid = dragCueParent.Parent as Grid;
                    if (wrapperGrid != null && wrapperGrid.Background != transparentBrush)
                    {
                        wrapperGrid.Background = transparentBrush;
                        backgroundApplied = true;
                    }
                }


            }

            if (args.Options.Status == DragStatus.DragComplete && args.Options.Status == DragStatus.DragCancel)
            {
                backgroundApplied = false;
            }
        }

        private static bool backgroundApplied;

    }
    

}
