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 TreeViewDNDWithRia.Web;
using TreeViewDNDWithRia.ViewModels;
using System.Collections.ObjectModel;
using System.ServiceModel.DomainServices.Client;
using Telerik.Windows.Controls.DragDrop;
using Telerik.Windows;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.TreeView;

namespace TreeViewDNDWithRia
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.DataContext = new TreeViewViewModel();
            this.treeView.AddHandler(RadDragAndDropManager.DropQueryEvent, new EventHandler<DragDropQueryEventArgs>(OnDropQuery), true);
            this.treeView.AddHandler(RadDragAndDropManager.DragQueryEvent, new EventHandler<DragDropQueryEventArgs>(OnDragQuery), true);
            this.treeView.PreviewDragEnded += new RadTreeViewDragEndedEventHandler(TreeView_PreviewDragEnded);
            this.treeView.DragOverTree += new DragOverTreeEventHandler(TreeView_DragOverTree);
        }

        void TreeView_DragOverTree(object sender, DragOverTreeEventArgs e)
        {
            TreeViewDragCue dragCue = (RadDragAndDropManager.Options.DragCue as TreeViewDragCue);
            if (e.TargetDropItem.Item is CategoryViewModel)
            {
                if (e.DropPosition == DropPosition.After ||
                    e.DropPosition == DropPosition.Before)
                {
                    dragCue.DragTooltipVisibility = Visibility.Collapsed;
                }
                if (e.DropPosition == DropPosition.Inside)
                {
                    dragCue.DragTooltipVisibility = Visibility.Visible;
                }
            }
            else if (e.TargetDropItem.Item is ProductViewModel)
            {
                dragCue.DragTooltipVisibility = Visibility.Visible;
            }            
        }

        void TreeView_PreviewDragEnded(object sender, RadTreeViewDragEndedEventArgs e)
        {
            if (e.TargetDropItem.Item is CategoryViewModel)
            {
                if (e.DropPosition == DropPosition.After ||
                    e.DropPosition == DropPosition.Before)
                {
                    e.Handled = true;
                }
            }
        }

        // OnDragQuery event handler
        private void OnDragQuery(object sender, DragDropQueryEventArgs e)
        {
            RadTreeViewItem draggedItem = e.Options.Source as RadTreeViewItem;
            if (draggedItem.Item is CategoryViewModel)
            {
                e.QueryResult = false;
            }
        }

        // OnDropQuery event handler
        private void OnDropQuery(object sender, DragDropQueryEventArgs e)
        {
            if (e.Options.Destination == this.treeView)
            {
                e.QueryResult = false;
            }
        }
    }
}
