// GeometryLibrary.cs
// Copyright (c) 2015 Bentley Systems, Incorporated. All Rights Reserved.

using System.Drawing;
using System.Windows.Controls.Primitives;

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// Class provides static utility methods for working with basic geometry.
    /// </summary>
    internal sealed class GeometryLibrary
    {
        #region Constructor
        private GeometryLibrary()
        {
        }
        #endregion

        #region Internal Static Methods
        internal static System.Windows.Point GetWindowsPoint(Point point)
        {
            return new System.Windows.Point(point.X, point.Y);
        }

        internal static System.Windows.Rect GetRect(Rectangle rectangle)
        {
            return new System.Windows.Rect(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
        }

        /// <summary>
        /// Calculates the Rect for a given FrameworkElement.
        /// </summary>
        /// <param name="frameworkElement">The framework element to get the Rect for, relative to itself.</param>
        /// <returns>The calculated Rect for the element.</returns>
        internal static System.Windows.Rect GetRect(System.Windows.FrameworkElement frameworkElement)
        {
            return GetRect(frameworkElement, frameworkElement);
        }

        /// <summary>
        /// Calculates the Rect for a given FrameworkElement.
        /// </summary>
        /// <param name="frameworkElement">The framework element to get the Rect for.</param>
        /// <param name="relativeTo">The framework element to get the Rect relative to.</param>
        /// <returns>The calculated Rect for the element.</returns>
        internal static System.Windows.Rect GetRect(System.Windows.FrameworkElement frameworkElement, System.Windows.FrameworkElement relativeTo)
        {
            // http://stackoverflow.com/questions/3472388/determine-the-bounding-rect-of-a-wpf-element-relative-to-some-parent
            return frameworkElement.TransformToVisual(relativeTo).TransformBounds(LayoutInformation.GetLayoutSlot(frameworkElement));
        }

        internal static Rectangle GetRectangle(RECT rect)
        {
            return new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
        }

        internal static bool RectContains(System.Windows.Rect rect, System.Windows.Point point)
        {
            if (rect.X > point.X) // Rect to right of point
                return false;

            if (rect.Y > point.Y) // Rect below point
                return false;

            if (rect.Right < point.X) // Rect to the left of point
                return false;

            if (rect.Bottom < point.Y) // Rect above point
                return false;

            return true;
        }
        #endregion
    }
}
