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 Telerik.Windows.Controls.DragDrop;
using System.Collections;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.TreeView;
using System.Collections.ObjectModel;
using Telerik.Windows.Data;
using Telerik.Windows.Controls.GridView;

namespace SL.DragBetweenGrids
{
	public partial class MainPage : UserControl
	{
		public MainPage()
		{
			InitializeComponent();

			this.RadGridView1.ItemsSource = new RadObservableCollection<Product>(from c in Enumerable.Range(0, 10) select new Product { ID = c, Name = "Name" + c, UnitPrice = c });

			this.RadGridView2.ItemsSource = new RadObservableCollection<Product>(from c in Enumerable.Range(10, 20) select new Product { ID = c, Name = "Name" + c, UnitPrice = c });


			RadDragAndDropManager.AddDragQueryHandler(RadGridView1, OnGV1DragQuery);
			RadDragAndDropManager.AddDragInfoHandler(RadGridView1, OnGV1DragInfo);

			RadDragAndDropManager.AddDropQueryHandler(RadGridView2, OnGV2DropQuery);
			RadDragAndDropManager.AddDropInfoHandler(RadGridView2, OnGV2DropInfo); 
		}

		private void OnGV1DragQuery(object sender, DragDropQueryEventArgs e)
		{
			if (e.Options.Source is GridViewHeaderCell || e.Options.Source is GridViewGroupPanelCell)
				return;

			RadGridView gridView = sender as RadGridView;
			if (gridView != null)
			{
				IList selectedItems = gridView.SelectedItems.ToList();
				e.QueryResult = selectedItems.Count > 0;
				e.Options.Payload = selectedItems;
			}
			var cue= new TreeViewDragCue();
			cue.DragActionContent = "I AM BEING DRAGGING...";

			e.Options.DragCue = cue;
			e.QueryResult = true;
			e.Handled = true;
		}


		private void OnGV1DragInfo(object sender, DragDropEventArgs e)
		{
			if (e.Options.Source is GridViewHeaderCell || e.Options.Source is GridViewGroupPanelCell)
				return;

			RadGridView gridView = sender as RadGridView;
			IEnumerable draggedItems = e.Options.Payload as IEnumerable;

			if (e.Options.Status == DragStatus.DragInProgress)
			{
				//Set up a drag cue:  
				TreeViewDragCue cue = new TreeViewDragCue();
				//Here we need to choose a template for the items:  
				cue.ItemTemplate = this.Resources["ProductTemplate"] as DataTemplate;
				cue.ItemsSource = draggedItems;
				e.Options.DragCue = cue;
			}
			else if (e.Options.Status == DragStatus.DragComplete)
			{
				IList source = gridView.ItemsSource as IList;
				foreach (object draggedItem in draggedItems)
				{
					source.Remove(draggedItem);
				}
			}
		}




		private void OnGV2DropQuery(object sender, DragDropQueryEventArgs e)
		{
			if (e.Options.Source is GridViewHeaderCell || e.Options.Source is GridViewGroupPanelCell)
				return;

			// We allow drop only if the dragged items are products:  
			ICollection draggedItems = e.Options.Payload as ICollection;
			bool result = draggedItems.Cast<object>().All((object item) => item is Product);
			e.QueryResult = result;
			e.Handled = true;

			if (result)
			{
				TreeViewDragCue cue = e.Options.DragCue as TreeViewDragCue;
				cue.IsDropPossible = true;
			}

			// Note that here we agree to accept a drop. We will be notified  
			// in the DropInfo event whether a drop is actually possible.  

		}

		private void OnGV2DropInfo(object sender, DragDropEventArgs e)
		{
			if (e.Options.Source is GridViewHeaderCell || e.Options.Source is GridViewGroupPanelCell)
				return;

			RadGridView order = e.Options.Destination as RadGridView;
			ICollection draggedItems = e.Options.Payload as ICollection;

			// Get the drag cu that the TreeView or we have created  
			TreeViewDragCue cue = e.Options.DragCue as TreeViewDragCue;

			if (e.Options.Status == DragStatus.DropPossible)
			{
				// Set a suitable text:  
				string newString;
				newString = String.Format("Add {0}", draggedItems.Count);
				cue.DragActionContent = newString;

				cue.IsDropPossible = true;
				order.Background = this.Resources["DropPossibleBackground"] as Brush;
			}
			else if (e.Options.Status == DragStatus.DropImpossible)
			{
				cue.DragActionContent = null;
				cue.IsDropPossible = false;
			}
			else if (e.Options.Status == DragStatus.DropComplete)
			{
				IList items = order.ItemsSource as IList;
				foreach (object draggedItem in draggedItems)
				{
					items.Add(draggedItem);
				}
			}

			if (e.Options.Status != DragStatus.DropPossible)
			{
				order.Background = new SolidColorBrush(Colors.White);
			}



		}  
	}

	public class Product
	{
		public int ID { get; set; }
		public string Name { get; set; }
		public double UnitPrice { get; set; }
	}
}
