// EnvironmentLibrary.cs
// Copyright (c) 2018 Bentley Systems, Incorporated. All Rights Reserved.

using System;
using System.Drawing;
using System.Windows.Forms;
using Haestad.Support.Support;

namespace Haestad.Framework.UI.Integration
{
    /// <summary>
    /// Class provides static utility methods for obtaining information about the active environment.
    /// </summary>
    internal sealed class EnvironmentLibrary
    {
        #region Constructor
        private EnvironmentLibrary()
        {
        }
        #endregion

        #region Internal Static Methods
        /// <summary>
        /// Returns the "legacy" (prior to Windows 8.1) system wide vertical scaling factor.
        /// From Windows 8.1 onwards, scaling factor can be different on different monitors.
        /// </summary>
        /// <returns>The scaling factor relative to 100% scale.</returns>
        internal static double GetSystemVerticalScalingFactor()
        {
            double scalingFactor = 0.0;
            using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
            {
                IntPtr desktopHdc = graphics.GetHdc();
                double pixelsPerInchY = Win32Library.GetDeviceCaps(desktopHdc, (int)Win32Library.DEVICE_CAPS.LOGPIXELSY);
                const double PIXELS_PER_INCH_Y_DEFAULT = 96.0; // 96 DPI = 100%
                scalingFactor = pixelsPerInchY / PIXELS_PER_INCH_Y_DEFAULT;
                graphics.ReleaseHdc(desktopHdc);
            }
            return scalingFactor;
        }

        /// <summary>
        /// Returns the "legacy" (prior to Windows 8.1) system wide horizontal scaling factor.
        /// From Windows 8.1 onwards, scaling factor can be different on different monitors.
        /// </summary>
        /// <returns>The scaling factor relative to 100% scale.</returns>
        internal static double GetSystemHorizontalScalingFactor()
        {
            double scalingFactor = 0.0;
            using (Graphics graphics = Graphics.FromHwnd(IntPtr.Zero))
            {
                IntPtr desktopHdc = graphics.GetHdc();
                double pixelsPerInchX = Win32Library.GetDeviceCaps(desktopHdc, (int)Win32Library.DEVICE_CAPS.LOGPIXELSX);
                const double PIXELS_PER_INCH_X_DEFAULT = 96.0; // 96 DPI = 100%
                scalingFactor = pixelsPerInchX / PIXELS_PER_INCH_X_DEFAULT;
                graphics.ReleaseHdc(desktopHdc);
            }
            return scalingFactor;
        }
        #endregion

        #region Private Static Methods
        private static void InitializeOSVersion()
        {
            Win32Library.GetOperatingSystemVersion(out m_osMajorVersion, out m_osMinorVersion);
        }
        #endregion

        #region Internal Static Properties
        /// <summary>
        /// This width represents the pixel width between the parent form edge (which is all client area)
        /// and the location/width/height of the TelerikWPFRibbonHost. Since the host swallows windows messages, this
        /// width also represents the available real-estate for form border sizing hot spots.
        /// </summary>
        internal static int NonClientAreaBorderWidth
        {
            get
            {
                const int DEFAULT_CLIENT_BORDER_WIDTH = 10;
                return Math.Max(SystemInformation.BorderSize.Width, DEFAULT_CLIENT_BORDER_WIDTH);
            }
        }

        internal static int VisibleBorderWidth
        {
            get { return 1; }
        }

        internal static int TitleBarButtonPadding
        {
            get { return 0; }
        }

        internal static int ApplicationIconButtonPadding
        {
            get { return 4; }
        }

        internal static string GetOSVersionString()
        {
            if (IsWindows10)
                return TextManager.Current["hfui_Windows10"];
            if (IsWindows8PointOne)
                return TextManager.Current["hfui_Windows8Point1"];
            if (IsWindows8)
                return TextManager.Current["hfui_Windows8"];
            if (IsWindows7)
                return TextManager.Current["hfui_Windows7"];
            if (IsWindowsVista)
                return TextManager.Current["hfui_WindowsVista"];
            if (IsWindowsXP)
                return TextManager.Current["hfui_WindowsXP"];

            return TextManager.Current["hfui_Windows10SuccessorOrUnknown"];
        }

        internal static bool IsWindows10OrSuccessor
        {
            get
            {
                // 10.0+
                if (OSMajorVersion >= 10)
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// Check if the operating system is Windows 10.
        /// </summary>
        internal static bool IsWindows10
        {
            get
            {
                // 10.0
                if (OSMajorVersion == 10 && OSMinorVersion == 0)
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// Check if the operating system is Windows 8.1 / Windows Server 2012 R2
        /// </summary>
        internal static bool IsWindows8PointOne
        {
            get
            {
                // 6.3 (only)
                if (OSMajorVersion == 6 && OSMinorVersion == 3)
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// Check if the operating system is Windows 8 / Windows Server 2012
        /// </summary>
        internal static bool IsWindows8
        {
            get
            {
                // 6.2 (only)
                if (OSMajorVersion == 6 && OSMinorVersion == 2)
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// Check if the operating system is Windows 7 / Server 2008 R2
        /// </summary>
        internal static bool IsWindows7
        {
            get
            {
                // 6.1 (only)
                if (OSMajorVersion == 6 && OSMinorVersion == 1)
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// Check if the operating system is Windows Vista / Server 2008
        /// </summary>
        internal static bool IsWindowsVista
        {
            get
            {
                // 6.0 (only)
                if (OSMajorVersion == 6 && OSMinorVersion == 0)
                    return true;
                else
                    return false;
            }
        }

        /// <summary>
        /// Check if the operating system is Windows XP / Server 2003 / Server 2003 R2
        /// </summary>
        internal static bool IsWindowsXP
        {
            get
            {
                // 5.1 (XP), 5.2 (XP x64)
                if (OSMajorVersion == 5 && OSMinorVersion >= 1)
                    return true;
                else
                    return false;
            }
        }
        
        /// <summary>
        /// The operating system major version number.
        /// </summary>
        internal static int OSMajorVersion
        {
            get
            {
                if (m_osMajorVersion == Int32.MinValue)
                    InitializeOSVersion();
                return m_osMajorVersion;
            }
        }

        /// <summary>
        /// The operating system minor version number.
        /// </summary>
        internal static int OSMinorVersion
        {
            get
            {
                if (m_osMinorVersion == Int32.MinValue)
                    InitializeOSVersion();
                return m_osMinorVersion;
            }
        }
        #endregion

        #region Private Static Fields
        private static int m_osMajorVersion = Int32.MinValue;
        private static int m_osMinorVersion = Int32.MinValue;
        #endregion
    }
}
