using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Interop;

namespace PDF_Master.Helper
{
    /// <summary>
    /// 显示拖拽时图片的辅助类
    /// </summary>
    public static class DragDropHelper
    {
        private static IDropTargetHelper helper;

        /// <summary>
        /// 拖拽相关操作的实例
        /// </summary>
        public static IDropTargetHelper Helper
        {
            get
            {
                if (helper == null)
                {
                    helper = Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("4657278A-411B-11D2-839A-00C04FD918D0"))) as IDropTargetHelper;
                }
                return helper;
            }
            set { helper = value; }
        }


        public static void DragEnter(FrameworkElement element, DragEventArgs e)
        {
            try
            {
                Point iconPoint = e.GetPosition(element);
                Helper?.DragEnter(new WindowInteropHelper(Window.GetWindow(element)).Handle, (System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
            }
            catch { }
        }

        public static void DragOver(FrameworkElement element, DragEventArgs e)
        {
            try
            {
                Point iconPoint = e.GetPosition(element);
                Helper?.DragOver(ref iconPoint, e.Effects);
            }
            catch { }
        }

        public static void DragLeave()
        {
            try
            {
                Helper?.DragLeave();
            }
            catch { }
        }

        public static void Drop(FrameworkElement element, DragEventArgs e)
        {
            try
            {
                Point iconPoint = e.GetPosition(element);
                Helper?.Drop((System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref iconPoint, e.Effects);
            }
            catch { }
        }
    }

    /// <summary>
    /// 系统设置拖拽时图片的接口
    /// </summary>
    [ComImport]
    [GuidAttribute("4657278B-411B-11d2-839A-00C04FD918D0")]
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IDropTargetHelper
    {
        // Notifies the drag-image manager that the drop target's IDropTarget::DragEnter method has been called  
        [PreserveSig]
        Int32 DragEnter(IntPtr hwndTarget, System.Runtime.InteropServices.ComTypes.IDataObject pDataObject, ref Point ppt, DragDropEffects dwEffect);

        // Notifies the drag-image manager that the drop target's IDropTarget::DragLeave method has been called  
        [PreserveSig]
        Int32 DragLeave();

        // Notifies the drag-image manager that the drop target's IDropTarget::DragOver method has been called  
        [PreserveSig]
        Int32 DragOver(ref Point ppt, DragDropEffects dwEffect);

        // Notifies the drag-image manager that the drop target's IDropTarget::Drop method has been called  
        [PreserveSig]
        Int32 Drop(System.Runtime.InteropServices.ComTypes.IDataObject pDataObject, ref Point ppt, DragDropEffects dwEffect);

        // Notifies the drag-image manager to show or hide the drag image  
        [PreserveSig]
        Int32 Show(bool fShow);
    }
}