// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information. 
namespace System.Reactive.Concurrency
{
    /// 
    /// Abstract base class for machine-local schedulers, using the local system clock for time-based operations.
    /// 
    public abstract partial class LocalScheduler : IScheduler, IStopwatchProvider, IServiceProvider
    {
        /// 
        /// Gets the scheduler's notion of current time.
        /// 
        public virtual DateTimeOffset Now => Scheduler.Now;
        /// 
        /// Schedules an action to be executed.
        /// 
        /// The type of the state passed to the scheduled action.
        /// State passed to the action to be executed.
        /// Action to be executed.
        /// The disposable object used to cancel the scheduled action (best effort).
        ///  is null.
        public virtual IDisposable Schedule(TState state, Func action)
        {
            if (action == null)
                throw new ArgumentNullException(nameof(action));
            return Schedule(state, TimeSpan.Zero, action);
        }
        /// 
        /// Schedules an action to be executed after dueTime.
        /// 
        /// The type of the state passed to the scheduled action.
        /// State passed to the action to be executed.
        /// Action to be executed.
        /// Relative time after which to execute the action.
        /// The disposable object used to cancel the scheduled action (best effort).
        public abstract IDisposable Schedule(TState state, TimeSpan dueTime, Func action);
        /// 
        /// Schedules an action to be executed at dueTime.
        /// 
        /// The type of the state passed to the scheduled action.
        /// State passed to the action to be executed.
        /// Action to be executed.
        /// Absolute time at which to execute the action.
        /// The disposable object used to cancel the scheduled action (best effort).
        ///  is null.
        public virtual IDisposable Schedule(TState state, DateTimeOffset dueTime, Func action)
        {
            if (action == null)
                throw new ArgumentNullException(nameof(action));
            return Enqueue(state, dueTime, action);
        }
        /// 
        /// Starts a new stopwatch object.
        /// 
        /// New stopwatch object; started at the time of the request.
        /// 
        /// Platform-specific scheduler implementations should reimplement 
        /// to provide a more efficient  implementation (if available).
        /// 
        public virtual IStopwatch StartStopwatch() => ConcurrencyAbstractionLayer.Current.StartStopwatch();
        object IServiceProvider.GetService(Type serviceType) => GetService(serviceType);
        /// 
        /// Discovers scheduler services by interface type. The base class implementation returns
        /// requested services for each scheduler interface implemented by the derived class. For
        /// more control over service discovery, derived types can override this method.
        /// 
        /// Scheduler service interface type to discover.
        /// Object implementing the requested service, if available; null otherwise.
        protected virtual object GetService(Type serviceType)
        {
            if (serviceType == typeof(IStopwatchProvider))
                return this as IStopwatchProvider;
            else if (serviceType == typeof(ISchedulerLongRunning))
                return this as ISchedulerLongRunning;
            else if (serviceType == typeof(ISchedulerPeriodic))
                return this as ISchedulerPeriodic;
            return null;
        }
    }
}