using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Ramitta.Utils
{
    public class WindowTopArea : System.Windows.Controls.Border
    {
        static WindowTopArea()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowTopArea), new FrameworkPropertyMetadata(typeof(WindowTopArea)));
        }

        private new bool CaptureMouse = false;
        private enum ResizePosition
        { None, Left, TopLeft, Top, TopRight, Right, BottomRight, Bottom, BottomLeft }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            Window win = Window.GetWindow(this);
            if (e.ClickCount == 1 && e.LeftButton == MouseButtonState.Pressed)
            {
                win.DragMove();
            }
            else if (e.ClickCount == 2)
            {
                win.SizeToContent = SizeToContent.Manual;
                win.WindowState = win.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;
            }
            base.OnMouseLeftButtonDown(e);
            e.Handled = true;
        }

        protected override void OnInitialized(EventArgs e)
        {
            SetWindowResizer();
            base.OnInitialized(e);
        }

        #region 窗体大小变化
        public void SetWindowResizer()
        {
            Window win = Window.GetWindow(this);
            ResizePosition ResPosition = ResizePosition.None;
            int Resizer = 10;
            int ResizerSpeed = 10;
            win.MouseMove += new MouseEventHandler(
                    delegate (object target, MouseEventArgs args)
                    {
                        try
                        {
                            //do resize
                            if (win.WindowState == WindowState.Normal)
                            {
                                Point MS = args.GetPosition(win);
                                if (args.LeftButton == MouseButtonState.Pressed)
                                {
                                    Win32.POINT pos = new Win32.POINT();
                                    Win32.GetCursorPos(out pos);
                                    #region 改变窗体大小
                                    switch (ResPosition)
                                    {
                                        case ResizePosition.Left:
                                            //左边                          
                                            Mouse.SetCursor(Cursors.SizeWE);
                                            Point transPointLeft = win.PointToScreen(new Point(0, 0));
                                            win.Left += pos.X - transPointLeft.X;
                                            win.Width += transPointLeft.X - pos.X;
                                            break;
                                        case ResizePosition.Right:
                                            //右边
                                            Mouse.SetCursor(Cursors.SizeWE);
                                            Point transPointRight = win.PointToScreen(new Point(win.Width, 0));
                                            win.Width += pos.X - transPointRight.X;
                                            break;
                                        case ResizePosition.Top:
                                            //顶部
                                            Mouse.SetCursor(Cursors.SizeNS);
                                            Point transPointTop = win.PointToScreen(new Point(0, 0));
                                            win.Top += pos.Y - transPointTop.Y;
                                            win.Height += transPointTop.Y - pos.Y;
                                            break;
                                        case ResizePosition.Bottom:
                                            //底部
                                            Mouse.SetCursor(Cursors.SizeNS);
                                            Point transPointBottom = win.PointToScreen(new Point(0, win.Height));
                                            win.Height += pos.Y - transPointBottom.Y;
                                            break;
                                        case ResizePosition.TopLeft:
                                            //左上
                                            Mouse.SetCursor(Cursors.SizeNWSE);
                                            Point transPointTopLeft = win.PointToScreen(new Point(0, 0));
                                            win.Left += pos.X - transPointTopLeft.X;
                                            win.Top += pos.Y - transPointTopLeft.Y;
                                            win.Width += transPointTopLeft.X - pos.X;
                                            win.Height += transPointTopLeft.Y - pos.Y;
                                            break;
                                        case ResizePosition.BottomLeft:
                                            //左下
                                            Mouse.SetCursor(Cursors.SizeNESW);
                                            Point transPointBottomLeft = win.PointToScreen(new Point(0, win.Height));
                                            win.Left += pos.X - transPointBottomLeft.X;
                                            win.Width += transPointBottomLeft.X - pos.X;
                                            win.Height += pos.Y - transPointBottomLeft.Y;
                                            break;
                                        case ResizePosition.TopRight:
                                            //右上
                                            Mouse.SetCursor(Cursors.SizeNESW);
                                            Point transPointTopRight = win.PointToScreen(new Point(win.Width, 0));
                                            win.Top += pos.Y - transPointTopRight.Y;
                                            win.Width = transPointTopRight.Y - pos.X;
                                            win.Height = transPointTopRight.Y - pos.Y;
                                            break;
                                        case ResizePosition.BottomRight:
                                            //右下
                                            Mouse.SetCursor(Cursors.SizeNWSE);
                                            Point transPointBottomRight = win.PointToScreen(new Point(win.Width, win.Height));
                                            win.Width += pos.X - transPointBottomRight.X;
                                            win.Height += pos.Y - transPointBottomRight.Y;
                                            break;
                                        case ResizePosition.None:
                                        default:
                                            Mouse.SetCursor(Cursors.Arrow);
                                            break;
                                    }
                                    #endregion
                                }
                                /*
                                else if (MS.X <= Resizer + ResizerSpeed && MS.Y <= Resizer + ResizerSpeed)
                                {
                                    //左上 (不执行)
                                    Mouse.SetCursor(Cursors.SizeNWSE);
                                    ResPosition = ResizePosition.TopLeft;
                                }
                                */
                                else if (MS.X <= Resizer && MS.Y >= win.ActualHeight - Resizer)
                                {
                                    //左下
                                    Mouse.SetCursor(Cursors.SizeNESW);
                                    ResPosition = ResizePosition.BottomLeft;
                                }
                                /*
                                else if (MS.X >= win.ActualWidth - Resizer - ResizerSpeed && MS.Y <= Resizer + ResizerSpeed)
                                {
                                    //右上(不执行)
                                    Mouse.SetCursor(Cursors.SizeNESW);
                                    ResPosition = ResizePosition.TopRight;
                                }
                                */
                                else if (MS.X >= win.ActualWidth - Resizer && MS.Y >= win.ActualHeight - Resizer)
                                {
                                    //右下
                                    Mouse.SetCursor(Cursors.SizeNWSE);
                                    ResPosition = ResizePosition.BottomRight;
                                }
                                else if (MS.X <= Resizer)
                                {
                                    //左边
                                    Mouse.SetCursor(Cursors.SizeWE);
                                    ResPosition = ResizePosition.Left;
                                }
                                /*
                                else if (MS.Y <= Resizer + ResizerSpeed)
                                {
                                    //顶部(不执行)
                                    Mouse.SetCursor(Cursors.SizeNS);
                                    ResPosition = ResizePosition.Top;
                                }
                                */
                                else if (MS.X >= win.ActualWidth - Resizer)
                                {
                                    //右边
                                    Mouse.SetCursor(Cursors.SizeWE);
                                    ResPosition = ResizePosition.Right;
                                }
                                else if (MS.Y >= win.ActualHeight - Resizer)
                                {
                                    //底部
                                    Mouse.SetCursor(Cursors.SizeNS);
                                    ResPosition = ResizePosition.Bottom;
                                }
                                else
                                {
                                    //无
                                    Mouse.SetCursor(Cursors.Arrow);
                                    ResPosition = ResizePosition.None;
                                }
                            }
                        }
                        catch
                        {
                            ResPosition = ResizePosition.None;
                            win.ReleaseMouseCapture();
                        }
                        args.Handled = CaptureMouse;
                    }
                );
            win.MouseLeftButtonDown += new MouseButtonEventHandler(
                    delegate (object target, MouseButtonEventArgs args)
                    {
                        if (win.WindowState == WindowState.Normal)
                        {
                            //获取当前鼠标点击点相对于Dvap.Shell窗体的位置
                            Point pos = args.GetPosition(win);
                            if (ResPosition != ResizePosition.None)
                            {
                                CaptureMouse = win.CaptureMouse();
                            }
                            args.Handled = CaptureMouse;
                        }
                    }
                );
            win.MouseLeftButtonUp += new MouseButtonEventHandler(
                    delegate (object target, MouseButtonEventArgs args)
                    {
                        if (win.WindowState == WindowState.Normal)
                        {
                            ResPosition = ResizePosition.None;
                            if (CaptureMouse)
                            {
                                win.ReleaseMouseCapture();
                                CaptureMouse = false;
                            }
                            args.Handled = CaptureMouse;
                        }
                    }
                );
        }
        #endregion
    }
}
