using C1.Xaml;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace TreeViewSamples
{
    /// <summary>
    /// Represents a Helper class used to implement the custom Drag&Drop functionality
    /// for a C1TreeViewItem.
    /// </summary>
    public class CustomDragDrop
    {
        /// <summary>
        /// Initializes a new instance of a <see cref="CustomDragDrop"/> object.
        /// </summary>
        /// <param name="tree"></param>
        public CustomDragDrop(C1TreeView tree)
        {
            Tree = tree;
            Mark = new DropMark();
            Effect = DragDropEffect.Move;
        }

        /// <summary>
        /// The C1TreeView that will use this custom Drag&Drop implementation.
        /// </summary>
        private C1TreeView Tree { get; set; }

        /// <summary>
        /// A custom visual element used to indicate what the target of the drop will be.
        /// </summary>
        private DropMark Mark { get; set; }

        /// <summary>
        /// The desired effect for the drop action (move or copy elements)
        /// </summary>
        public DragDropEffect Effect { get; set; }

        private int index;

        /// <summary>
        /// Used to handle the DragOver event.
        /// Adds the mark to indicate the drop target
        /// Determines if a given node of the tree is a valid drop location
        /// </summary>
        /// <param name="source">The source of the event</param>
        /// <param name="e">The event arguments</param>
        public void HandleDragOver(object source, C1DragDeltaEventArgs e)
        {
            C1DragDropManager dragDropManager = source as C1DragDropManager;
            if (Mark.Parent == null)
                dragDropManager.Canvas.Children.Add(Mark);
            Mark.Visibility = Visibility.Collapsed;
            e.Effect = DragDropEffect.None;

            // Check if is a valid drop action
            C1TreeViewItem target = Tree.GetNode(e.GetPosition(null));
            if (IsValidDrop(e.DragSource as C1TreeViewItem, target))
            {
                // get target with respect to drag/drop canvas
                Point p = target.C1TransformToVisual(dragDropManager.Canvas).TransformPoint(new Point());
                Point pos = e.GetPosition(target);
                if (pos.Y > target.RenderSize.Height / 2)
                {
                    p.Y += target.RenderSize.Height;
                }
                Mark.SetValue(Canvas.LeftProperty, p.X);
                Mark.SetValue(Canvas.TopProperty, p.Y);
                Mark.Visibility = Visibility.Visible;
                e.Effect = DragDropEffect.Move;
            }

            
        }

        /// <summary>
        /// Used to handle the DragDrop event.
        /// Determines the target and fires the corresponding action (move or copy).
        /// </summary>
        /// <param name="source">The source of the event</param>
        /// <param name="e">The event arguments</param>
        public void HandleDragDrop(object source, C1DragCompletedEventArgs e)
        {
            
        }
    }

    /// <summary>
    /// Specifies the action that will be performed when the user drops a drag source into a drop target element.
    /// </summary>
    public enum DragDropEffect
    {
        /// <summary>
        /// No action.
        /// </summary>
        None,
        /// <summary>
        /// Move the source element into the target element.
        /// </summary>
        Move,
        /// <summary>
        /// Copy the source element into the target element.
        /// </summary>
        Copy,
    }
}
