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.Documents;
using System.Windows.Input;
using System.Windows.Media;
using static PDF_Office.CustomControl.ColorDropBox;

namespace PDF_Office.Helper
{
    #region 悬浮窗口
    public class AdornerPresenter : Adorner
    {
        private VisualCollection VisualChildren;
        private ContentPresenter Content;
        public AdornerPresenter(UIElement adornedElement) : base(adornedElement)
        {
            VisualChildren = new VisualCollection(this);
            Content = new ContentPresenter();
            VisualChildren.Add(Content);
        }

        protected override Size MeasureOverride(Size constraint)
        {
            Content.Measure(constraint);
            return Content.DesiredSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            Content.Arrange(new Rect(0, 0, finalSize.Width, finalSize.Height));
            return Content.RenderSize;
        }

        protected override Visual GetVisualChild(int index)
        {
            return VisualChildren[index];
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return VisualChildren.Count;
            }
        }

        public object VisualContent
        {
            get
            {
                return Content.Content;
            }
            set
            {
                Content.Content = value;
            }
        }
    }

    //悬浮窗口
    //hwnd:鼠标点击区域所在的窗口或控件
    //targetPop:悬浮窗口
    //offset:位于某窗口的位置,默认为整个主窗口的位置

    //Helper.PopControlHelper popControlHelper;
    //        if (popControlHelper == null)
    //            popControlHelper = new Helper.PopControlHelper(sender as Control, new CustomControl.CompositeControl.SlidContentPop());
    //        else
    //            popControlHelper.UpdatePointOffset(Mouse.GetPosition(App.Current.MainWindow));

    //        popControlHelper.ShowLayer();

    public class PopControlHelper : UserControl
    {
        private AdornerLayer popLayer;
        private AdornerPresenter layerPresent;
        private bool layerAdded = false;
        private Canvas popCanvas;
        private UserControl TargetPop;//悬浮窗口
        private Control HwndControl;//悬浮窗口的上一级窗口,即鼠标点击的区域所在的窗口
        private Point offset;//悬浮窗口,位于某窗口的位置
        public PopControlHelper(Control hwnd, UserControl targetPop, Point offset)
        {
            this.offset = offset;
            HwndControl = hwnd;
            TargetPop = targetPop;
            InitData();
        }

        public PopControlHelper(Control hwnd, UserControl targetPop)
        {
            //默认为主窗口的位置
            offset = Mouse.GetPosition(App.Current.MainWindow);
            HwndControl = hwnd;
            TargetPop = targetPop;
            InitData();
        }

        public void UpdatePointOffset(Point point)
        {
            offset = point;
        }

        public void ShowLayer(DependencyObject dependencyObject = null)
        {
            if (layerAdded)
            {
                RemoveFromLayer();
                return;
            }
            AddToLayer(dependencyObject);
        }

        public void RemoveLayer()
        {
            if (layerAdded)
            {
                RemoveFromLayer();
                return;
            }
        }

        //绑定鼠标点击整个窗口区域的事件
        private void InitData()
        {
            if (popLayer == null)
            {
                Window parentWnd = Window.GetWindow(HwndControl);
                if (parentWnd != null && parentWnd.Content is UIElement && TargetPop != null)
                {
                    parentWnd.AddHandler(MouseDownEvent, new MouseButtonEventHandler((eventsender, param) =>
                    {
                        if (layerAdded)
                        {
                            try
                            {

                                Point p = Mouse.GetPosition(TargetPop);
                                double width = 0;
                                double height = 0;
                                if (double.IsNaN(TargetPop.Width) == false && double.IsNaN(TargetPop.Height) == false)
                                {
                                    width = TargetPop.Width;
                                    height = TargetPop.Height;
                                }
                                else
                                {
                                    width = TargetPop.ActualWidth;
                                    height = TargetPop.ActualHeight;
                                }

                                if (p.X < 0 || p.Y < 0 || p.X > width || p.Y > height)
                                {
                                    RemoveFromLayer();
                                }
                            }
                            catch (Exception ex)
                            {

                            }

                        }
                    }), true);
                }
            }
        }

        private void AddToLayer(DependencyObject dependencyObject = null)
        {
            Window parentWnd = Window.GetWindow(HwndControl);
            popLayer = AdornerLayer.GetAdornerLayer(parentWnd.Content as UIElement);
            if (popLayer != null && TargetPop != null && !layerAdded)
            {
                if (layerPresent == null)
                {
                    layerPresent = new AdornerPresenter(popLayer);
                }
                popLayer.Add(layerPresent);
                if (popCanvas == null)
                {
                    popCanvas = new Canvas();
                    popCanvas.Children.Add(TargetPop);
                    layerPresent.VisualContent = popCanvas;
                }

                TargetPop.Visibility = Visibility.Visible;
                TargetPop.Measure(new Size(parentWnd.ActualWidth, parentWnd.ActualHeight));
                TargetPop.SetValue(Canvas.TopProperty, offset.Y + HwndControl.ActualHeight + 2);

                if (offset.X + TargetPop.DesiredSize.Width + SystemParameters.ResizeFrameVerticalBorderWidth * 2 > parentWnd.ActualWidth)
                {
                    TargetPop.SetValue(Canvas.LeftProperty, parentWnd.ActualWidth - TargetPop.DesiredSize.Width - 5 - SystemParameters.ResizeFrameVerticalBorderWidth * 2);
                }
                else
                {
                    TargetPop.SetValue(Canvas.LeftProperty, offset.X);
                }
                layerAdded = true;
            }
        }
        private void RemoveFromLayer()
        {
            if (popLayer != null && layerPresent != null && layerAdded)
            {
                popLayer.Remove(layerPresent);
                layerAdded = false;
            }
        }

    }

    #endregion

    #region 自定义菜单
    //自定义菜单:事件绑定、内容等

    public class CustomPopMenu
    {
        public ContextMenu PopMenu { get; private set; }
        public List<MenuItem> MenuItems { get; private set; }

        public CustomPopMenu(ContextMenu popMenu)
        {
            PopMenu = popMenu;
            GetMenuItems();
        }

        private void GetMenuItems()
        {
            if (PopMenu == null || PopMenu.Items == null)
                return;

            MenuItems = new List<MenuItem>();

            foreach (var item in PopMenu.Items)
            {
                if (item as MenuItem != null)
                {
                    MenuItems.Add(item as MenuItem);
                }
            }
        }

        //显示或隐藏所有菜单项
        public void AllMenuVisibility(bool isVisual)
        {
            if (MenuItems == null) return;

            foreach (var item in MenuItems)
            {
                item.Visibility = (isVisual ? Visibility.Visible : Visibility.Collapsed);
            }
        }

        private MenuItem ContainterOfIndex(int index)
        {
            if (MenuItems == null || MenuItems.Count < index || index < 0)
                return null;

            MenuItem menuItem = MenuItems[index] as MenuItem;
            return menuItem;
        }

        private MenuItem ContainterOfTag(string tag)
        {
            if (MenuItems == null ||  string.IsNullOrEmpty(tag))
                return null;
            var menuItem = MenuItems.FirstOrDefault(temp => temp.Tag.ToString() == tag);
            return menuItem;
        }

        public void SetVisibilityProperty(int index, bool isVisual)
        {
            var menuItem = ContainterOfIndex(index);
            if (menuItem != null)
            {
                menuItem.Visibility = (isVisual?Visibility.Visible:Visibility.Collapsed);
            }
        }

        public void SetVisibilityProperty(string tag, bool isVisual)
        {
            var menuItem = ContainterOfTag(tag);
            if (menuItem != null)
            {
                menuItem.Visibility = (isVisual ? Visibility.Visible : Visibility.Collapsed);
            }
        }

        public void SetTagProperty(int index, string tag)
        {
            var menuItem = ContainterOfIndex(index);
            if (menuItem != null)
            {
                menuItem.Tag = tag;
            }
        }
        public void SetHeaderProperty(int index, string header)
        {
            var menuItem = ContainterOfIndex(index);

            if (menuItem != null)
            {
                menuItem.Header = header;
            }
        }

        public void SetMenuBinding(int index, object commandTarget, ICommand command)
        {
            var menuItem = ContainterOfIndex(index);

            if (menuItem != null)
            {
                menuItem.CommandTarget = (UIElement)commandTarget;
                menuItem.Command = command;
            }

        }

    }

    #endregion

}