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.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

// Notes: The Canvas must have a non-null background to make it generate mouse events.

namespace WpfMoveRectangles
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        // The part of the rectangle the mouse is over.
        private enum HitType
        {
            None, Body, UL, UR, LR, LL, L, R, T, B
        };

        // True if a drag is in progress.
        private bool DragInProgress = false;

        // The drag's last point.
        private Point LastPoint;

        // The part of the rectangle under the mouse.
        HitType MouseHitType = HitType.None;

        // The Rectangle that was hit.
        private Rectangle HitRectangle = null;

        // The Rectangles that the user can move and resize.
        private List<Rectangle> Rectangles;

        // Make a list of the Rectangles that the user can move.
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Rectangles = new List<Rectangle>();
            foreach (UIElement child in dragCanvas.Children)
            {
                if (child is Rectangle)
                    Rectangles.Add(child as Rectangle);
            }

            // Reverse the list so the Rectangles on top come first.
            Rectangles.Reverse();
        }

        // If the point is over any Rectangle, return the Rectangle and hit type.
        private void FindHit(Point point)
        {
            HitRectangle = null;
            MouseHitType = HitType.None;

            foreach (Rectangle rect in Rectangles)
            {
                MouseHitType = GetHitType(rect, point);
                if (MouseHitType != HitType.None)
                {
                    HitRectangle = rect;
                    return;
                }
            }

            // We didn't find a hit.
            return;
        }

        // Return a HitType indicating the part of the Rectangle at the Point.
        private HitType GetHitType(Rectangle rect, Point point)
        {
            double left = Canvas.GetLeft(rect);
            double top = Canvas.GetTop(rect);
            double right = left + rect.Width;
            double bottom = top + rect.Height;
            if (point.X < left) return HitType.None;
            if (point.X > right) return HitType.None;
            if (point.Y < top) return HitType.None;
            if (point.Y > bottom) return HitType.None;
            
            const double GAP = 10;
            if (point.X - left < GAP)
            {
                // Left edge.
                if (point.Y - top < GAP) return HitType.UL;
                if (bottom - point.Y < GAP) return HitType.LL;
                return HitType.L;
            }
            if (right - point.X < GAP)
            {
                // Right edge.
                if (point.Y - top < GAP) return HitType.UR;
                if (bottom - point.Y < GAP) return HitType.LR;
                return HitType.R;
            }
            if (point.Y - top < GAP) return HitType.T;
            if (bottom - point.Y < GAP) return HitType.B;
            return HitType.Body;
        }

        // Set a mouse cursor appropriate for the current hit type.
        private void SetMouseCursor()
        {
            // See what cursor we should display.
            Cursor desiredCursor = Cursors.Arrow;
            switch (MouseHitType)
            {
                case HitType.None:
                    desiredCursor = Cursors.Arrow;
                    break;
                case HitType.Body:
                    desiredCursor = Cursors.ScrollAll;
                    break;
                case HitType.UL:
                case HitType.LR:
                    desiredCursor = Cursors.SizeNWSE;
                    break;
                case HitType.LL:
                case HitType.UR:
                    desiredCursor = Cursors.SizeNESW;
                    break;
                case HitType.T:
                case HitType.B:
                    desiredCursor = Cursors.SizeNS;
                    break;
                case HitType.L:
                case HitType.R:
                    desiredCursor = Cursors.SizeWE;
                    break;
            }

            // Display the desired cursor.
            if (Cursor != desiredCursor) Cursor = desiredCursor;
        }

        // Start dragging.
        private void dragCanvas_MouseDown(object sender, MouseButtonEventArgs e)
        {
            FindHit(Mouse.GetPosition(dragCanvas));
            SetMouseCursor();
            if (MouseHitType == HitType.None) return;

            LastPoint = Mouse.GetPosition(dragCanvas);
            DragInProgress = true;
        }

        // If a drag is in progress, continue the drag.
        // Otherwise display the correct cursor.
        private void dragCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (!DragInProgress)
            {
                FindHit(Mouse.GetPosition(dragCanvas));
                SetMouseCursor();
            }
            else
            {
                // See how much the mouse has moved.
                Point point = Mouse.GetPosition(dragCanvas);
                double offsetX = point.X - LastPoint.X;
                double offsetY = point.Y - LastPoint.Y;

                // Get the rectangle's current position.
                double newX = Canvas.GetLeft(HitRectangle);
                double newY = Canvas.GetTop(HitRectangle);
                double newWidth = HitRectangle.Width;
                double newHeight = HitRectangle.Height;

                // Update the rectangle.
                switch (MouseHitType)
                {
                    case HitType.Body:
                        newX += offsetX;
                        newY += offsetY;
                        break;
                    case HitType.UL:
                        newX += offsetX;
                        newY += offsetY;
                        newWidth -= offsetX;
                        newHeight -= offsetY;
                        break;
                    case HitType.UR:
                        newY += offsetY;
                        newWidth += offsetX;
                        newHeight -= offsetY;
                        break;
                    case HitType.LR:
                        newWidth += offsetX;
                        newHeight += offsetY;
                        break;
                    case HitType.LL:
                        newX += offsetX;
                        newWidth -= offsetX;
                        newHeight += offsetY;
                        break;
                    case HitType.L:
                        newX += offsetX;
                        newWidth -= offsetX;
                        break;
                    case HitType.R:
                        newWidth += offsetX;
                        break;
                    case HitType.B:
                        newHeight += offsetY;
                        break;
                    case HitType.T:
                        newY += offsetY;
                        newHeight -= offsetY;
                        break;
                }

                // Don't use negative width or height.
                if ((newWidth > 0) && (newHeight > 0))
                {
                    // Update the rectangle.
                    Canvas.SetLeft(HitRectangle, newX);
                    Canvas.SetTop(HitRectangle, newY);
                    HitRectangle.Width = newWidth;
                    HitRectangle.Height = newHeight;

                    // Save the mouse's new location.
                    LastPoint = point;
                }
            }
        }

        // Stop dragging.
        private void dragCanvas_MouseUp(object sender, MouseButtonEventArgs e)
        {
            DragInProgress = false;
        }
    }
}
