namespace AyCode.Core
{
    /// <summary>
    /// Singleton!
    /// </summary>
    public sealed class AcDomain
    {
        private readonly object _forLock = new();  
        private static readonly AcDomain Instance = new();
        
        private int _uniqueInt32;
        private long _uniqueInt64;

        private string _processName;
        public string ProcessName => Instance._processName;

        static AcDomain()
        { }

        private AcDomain()
        {
            //AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
            //{
            //    var ex = (Exception) args?.ExceptionObject;

            //    if (ex == null) Instance._logger.Error($"GLOBAL EXCEPTION");
            //    else Instance._logger.Error($"GLOBAL EXCEPTION{Env.NL}{ex.Message}{Env.NL}{sender}", ex);
            //};

            //_processName = System.Diagnostics.Process.GetCurrentProcess()?.ProcessName ?? "Unknown ProcessName";
        }

        public static bool IsDeveloperVersion
        {
            get
            {
                #if PRODUCT
                {
                    return false;
                }
                #endif

                return true;
            }
        }

        public static bool IsProductVersion
        {
            get
            {
                #if PRODUCT
                {
                    return true;
                }
                #endif

                return false;
            }
        }

        //public static string CdnBaseUrl => IsProductVersion ? "https://cdn2.anataworld.com/" : "https://cdn2.anataworld.com/";


        /// <summary>
        /// 
        /// </summary>
        /// <param name="multiplier"></param>
        /// <returns>(int)(multiplier * Environment.ProcessorCount), but minimum: 1</returns>
        public static int ProcessorCount(double multiplier = 1)
        {
            var pCount = (int)(multiplier * Environment.ProcessorCount);
            
            return pCount > 0 ? pCount : ++pCount;
        }

        public static int NextUniqueInt32 => Interlocked.Increment(ref Instance._uniqueInt32);
        public static long NextUniqueInt64 => Interlocked.Increment(ref Instance._uniqueInt64);
    }
}