using Syncfusion.Windows.Tools.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SampleListTree
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        bool _draggingItem;
        bool _draggingItem2;
        Point _mouseStartPosition;
        public MainWindow()
        {
            InitializeComponent();
            PopulateListBox();
            PopulateTreeView();
        }
        private void PopulateListBox()
        {
            for (int i = 0; i <= 10; i++)
            {
                this.ListSource.Items.Add(DateTime.Now.AddDays(i));
                this.ListSource2.Items.Add(DateTime.Now.AddDays(i));
            }
        }

        private void PopulateTreeView()
        {
            for (int i = 1; i <= 2; i++)
            {
                TreeNode node = new TreeNode("Node" + i);
                for (int j = 1; j <= 2; j++)
                {
                    node.Nodes.Add("SubNode" + j);
                }
                this.TargetTree.Items.Add(node);
                this.TargetTree2.Items.Add(node);
            }
        }

        public T FindParent<T>(DependencyObject child)
                where T : DependencyObject
        {
                var parentObject = VisualTreeHelper.GetParent(child);

                if (parentObject == null)
                    return null;

                var parent = parentObject as T;
                if (parent != null)
                    return parent;

                return FindParent<T>(parentObject);

        }

        


        /// <summary>
        /// To get the parent of the item
        /// </summary>
        static TreeViewItemAdv GetParentItem(TreeViewItemAdv item)
        {
            for (var i = VisualTreeHelper.GetParent(item); i != null; i = VisualTreeHelper.GetParent(i))
                if (i is TreeViewItemAdv)
                    return (TreeViewItemAdv)i;

            return null;
        }
        private TreeViewItemAdv GetNearestContainer(UIElement element)
        {
            // Walk up the element tree to the nearest tree view item.
            TreeViewItemAdv container = element as TreeViewItemAdv;
            while ((container == null) && (element != null))
            {
                element = VisualTreeHelper.GetParent(element) as UIElement;
                container = element as TreeViewItemAdv;
            }
            return container;
        }


        private TreeViewItem GetNearestContainer2(UIElement element)
        {
            // Walk up the element tree to the nearest tree view item.
            TreeViewItem container = element as TreeViewItem;
            while ((container == null) && (element != null))
            {
                element = VisualTreeHelper.GetParent(element) as UIElement;
                container = element as TreeViewItem;
            }
            return container;
        }


        private void TargetTree_DragEnter(object sender, System.Windows.DragEventArgs e)
        {
            e.Effects = System.Windows.DragDropEffects.None;
        }

        private void TargetTree_Drop(object sender, System.Windows.DragEventArgs e)
        {
            // drag data
            // add item in target TreeViewItem 
            TreeViewItemAdv TargetItem = GetNearestContainer(e.OriginalSource as UIElement);

            TreeViewItemAdv item1 = new TreeViewItemAdv();
            item1.Header = "Added node";
            TargetItem.Items.Add(item1);
        }

        private void ListSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var listBoxItem = FindParent<ListBoxItem>((DependencyObject)e.OriginalSource);
            _draggingItem = listBoxItem != null;

            _mouseStartPosition = e.GetPosition(ListSource);
        }

        private void ListSource_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!_draggingItem)
                return;
            DragDrop.DoDragDrop(this.ListSource, this.ListSource.SelectedItem, System.Windows.DragDropEffects.Move);
            
        }

        private void ListSource2_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            var listBoxItem = FindParent<ListBoxItem>((DependencyObject)e.OriginalSource);
            _draggingItem2 = listBoxItem != null;

            _mouseStartPosition = e.GetPosition(ListSource);
        }
    

        private void ListSource2_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (!_draggingItem2)
                return;
            DragDrop.DoDragDrop(this.ListSource2, this.ListSource2.SelectedItem, System.Windows.DragDropEffects.Move);
    }

        private void TargetTree2_DragEnter(object sender, System.Windows.DragEventArgs e)
        {
            e.Effects = System.Windows.DragDropEffects.None;
        }

        private void TargetTree2_Drop(object sender, System.Windows.DragEventArgs e)
        {
            // drag data
            // add item in target TreeViewItem 
            TreeViewItem TargetItem = GetNearestContainer2(e.OriginalSource as UIElement);

            TreeViewItem item1 = new TreeViewItem();
            item1.Header = "Added node";
            TargetItem.Items.Add(item1);
        }

    }
}
