using System;
using System.Threading.Tasks;
using Volpe.Cafe.Utils;

namespace Volpe.Cafe.Model
{
    /// <summary>
    /// Internal class to support parallelism in the model.
    /// </summary>
    class Threading
    {
        static Threading()
        {
#if DEBUG
            WorkerCount = 1;
#else
            WorkerCount = (SystemInfo.ProcessorCount < 3)
                ? 1
                : Math.Min((int)Math.Round(Math.Sqrt(SystemInfo.ProcessorCount * 2)), 6);
#endif
        }

        /// <summary>
        /// Gets the max number of concurrent workers per task, based on system info.
        /// </summary>
        public static int WorkerCount { get; }

        /// <summary>
        /// Gets options to use in parallel for/foreach loops.
        /// </summary>
        public static ParallelOptions ParallelOptions => new ParallelOptions() { MaxDegreeOfParallelism = WorkerCount };
    }
}
