namespace RadToolBarDragAndDropItems
{
    using System.Windows;
    using System.Windows.Input;
    using Telerik.Windows.Controls;
    using Telerik.Windows.DragDrop;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public int DraggedButtonIndex { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            InitializeDragDropHandlers();
        }

        private void InitializeDragDropHandlers()
        {
            foreach (RadButton item in this.RadToolBar.Items)
            {
                DragDropManager.AddDragInitializeHandler(item, OnDragInitialize);
                DragDropManager.AddDragOverHandler(item, OnDragOver);
                DragDropManager.AddGiveFeedbackHandler(item, OnGiveFeedback);

                DragDropManager.AddDropHandler(item, OnDrop);
            }

            DragDropManager.AddDropHandler(RadToolBar, OnDrop);
        }

        private void OnDragInitialize(object sender, DragInitializeEventArgs args)
        {
            args.AllowedEffects = DragDropEffects.All;
            var payload = DragDropPayloadManager.GeneratePayload(null);
            payload.SetData("DragData", args.OriginalSource);
            args.Data = payload;

            // Add image as DragVisual
            var button = args.OriginalSource as RadButton;
            var draggingImage = new System.Windows.Controls.Image
            {
                Source = new Telerik.Windows.Media.Imaging.RadBitmap(button).Bitmap,
                Width = button.ActualWidth,
                Height = button.ActualHeight
            };
            args.DragVisual = draggingImage;

        }

        private void OnDragOver(object sender, Telerik.Windows.DragDrop.DragEventArgs e)
        {
            RadButton button = sender as RadButton;
            RadToolBar toolBar = button.ParentOfType<RadToolBar>();
            this.DraggedButtonIndex = toolBar.Items.IndexOf(button);
        }

        private void OnGiveFeedback(object sender, Telerik.Windows.DragDrop.GiveFeedbackEventArgs args)
        {
            args.SetCursor(Cursors.Arrow);
            args.Handled = true;
        }

        private void OnDrop(object sender, Telerik.Windows.DragDrop.DragEventArgs args)
        {
            RadButton draggedButton = ((DataObject)args.Data).GetData("DragData") as RadButton;

            if (args.Source is RadToolBar)
            {
                RadToolBar toolBar = args.Source as RadToolBar;
                if (toolBar != null)
                {
                    toolBar.Items.Remove(draggedButton);
                    toolBar.Items.Insert(this.DraggedButtonIndex, draggedButton);
                }
            }
            else if (sender is RadButton)
            {
                RadButton dragOverButton = sender as RadButton;
                RadToolBar toolBar = dragOverButton.ParentOfType<RadToolBar>();

                if (dragOverButton != null &&
                    toolBar != null)
                {
                    toolBar.Items.Remove(draggedButton);
                    toolBar.Items.Insert(this.DraggedButtonIndex, draggedButton);
                }
            }
        }
    }
}
