using System;
using System.Globalization;

namespace Volpe.Cafe.Utils
{
    /// <summary>
    /// Provides basic information about the current system.
    /// </summary>
    [Serializable]
    public static class SystemInfo
    {
        static SystemInfo()
        {
            ProcessorCount = Environment.ProcessorCount;
            ProcessorArchitecture = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
            ProcessorArchitecture_x86 = (ProcessorArchitecture == "x86" || ProcessorArchitecture == "X86");
            ProcessorArchitecture_x64 = (ProcessorArchitecture == "x64" || ProcessorArchitecture == "X64");

            MachineName = Environment.MachineName;
            OSVersion = Environment.OSVersion.ToString();
            CLRVersion = Environment.Version.ToString();
        }

        /// <summary>Gets the number of physical or logical processors (or CPU cores) in the system.</summary>
        public static int ProcessorCount { get; }
        /// <summary>Gets the processor architecture as identified by the system.</summary>
        public static string ProcessorArchitecture { get; }
        /// <summary>Gets whether the processor architecture is x86-32 as identified by the system.</summary>
        public static bool ProcessorArchitecture_x86 { get; }
        /// <summary>Gets whether the processor architecture is x86-64 as identified by the system.</summary>
        public static bool ProcessorArchitecture_x64 { get; }

        /// <summary>Gets the machine name of the current system.</summary>
        public static string MachineName { get; }
        /// <summary>Gets the Operating System name and version for the current system.</summary>
        public static string OSVersion { get; }
        /// <summary>Gets the Common Language Runtime version installed on the current system.</summary>
        public static string CLRVersion { get; }

        /// <summary>
        /// Gets the default culture information to use within the application domain of the CAFE Model.
        /// </summary>
        /// <remarks>
        /// The default culture is based on the "en-US" culture, but modifies the positive and negative infinity
        /// symbols to "Inf" and "-Inf" respectively.
        /// </remarks>
        public static CultureInfo DefaultCulture
        {
            get {
                CultureInfo defaultCulture = CultureInfo.CreateSpecificCulture("en-US");
                defaultCulture.NumberFormat.PositiveInfinitySymbol = "Inf";
                defaultCulture.NumberFormat.NegativeInfinitySymbol = "-Inf";
                return defaultCulture;
            }
        }

        internal static bool IsDefaultCultureSet { get; private set; }

        /// <summary>
        /// Sets the default culture information to use within the application domain of the CAFE Model.
        /// </summary>
        /// <seealso cref="DefaultCulture"/>
        public static void SetDefaultCulture()
        {
            if (!IsDefaultCultureSet) {
                CultureInfo.DefaultThreadCurrentCulture = DefaultCulture;
                IsDefaultCultureSet = true;
            }
        }

    }
}
