// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Reactive.Disposables;
namespace System.Reactive.Concurrency
{
    /// 
    /// Abstract base class for scheduled work items.
    /// 
    /// Absolute time representation type.
    public abstract class ScheduledItem : IScheduledItem, IComparable>
        where TAbsolute : IComparable
    {
        private readonly SingleAssignmentDisposable _disposable = new SingleAssignmentDisposable();
        private readonly TAbsolute _dueTime;
        private readonly IComparer _comparer;
        /// 
        /// Creates a new scheduled work item to run at the specified time.
        /// 
        /// Absolute time at which the work item has to be executed.
        /// Comparer used to compare work items based on their scheduled time.
        ///  is null.
        protected ScheduledItem(TAbsolute dueTime, IComparer comparer)
        {
            if (comparer == null)
                throw new ArgumentNullException("comparer");
            _dueTime = dueTime;
            _comparer = comparer;
        }
        /// 
        /// Gets the absolute time at which the item is due for invocation.
        /// 
        public TAbsolute DueTime
        {
            get { return _dueTime; }
        }
        /// 
        /// Invokes the work item.
        /// 
        public void Invoke()
        {
            if (!_disposable.IsDisposed)
                _disposable.Disposable = InvokeCore();
        }
        /// 
        /// Implement this method to perform the work item invocation, returning a disposable object for deep cancellation.
        /// 
        /// Disposable object used to cancel the work item and/or derived work items.
        protected abstract IDisposable InvokeCore();
        #region Inequality
        /// 
        /// Compares the work item with another work item based on absolute time values.
        /// 
        /// Work item to compare the current work item to.
        /// Relative ordering between this and the specified work item.
        /// The inequality operators are overloaded to provide results consistent with the IComparable implementation. Equality operators implement traditional reference equality semantics.
        public int CompareTo(ScheduledItem other)
        {
            // MSDN: By definition, any object compares greater than null, and two null references compare equal to each other. 
            if (object.ReferenceEquals(other, null))
                return 1;
            return _comparer.Compare(DueTime, other.DueTime);
        }
        /// 
        /// Determines whether one specified ScheduledItem<TAbsolute> object is due before a second specified ScheduledItem<TAbsolute> object.
        /// 
        /// The first object to compare.
        /// The second object to compare.
        /// true if the DueTime value of left is earlier than the DueTime value of right; otherwise, false.
        /// This operator provides results consistent with the IComparable implementation.
        public static bool operator <(ScheduledItem left, ScheduledItem right)
        {
            return Comparer>.Default.Compare(left, right) < 0;
        }
        /// 
        /// Determines whether one specified ScheduledItem<TAbsolute> object is due before or at the same of a second specified ScheduledItem<TAbsolute> object.
        /// 
        /// The first object to compare.
        /// The second object to compare.
        /// true if the DueTime value of left is earlier than or simultaneous with the DueTime value of right; otherwise, false.
        /// This operator provides results consistent with the IComparable implementation.
        public static bool operator <=(ScheduledItem left, ScheduledItem right)
        {
            return Comparer>.Default.Compare(left, right) <= 0;
        }
        /// 
        /// Determines whether one specified ScheduledItem<TAbsolute> object is due after a second specified ScheduledItem<TAbsolute> object.
        /// 
        /// The first object to compare.
        /// The second object to compare.
        /// true if the DueTime value of left is later than the DueTime value of right; otherwise, false.
        /// This operator provides results consistent with the IComparable implementation.
        public static bool operator >(ScheduledItem left, ScheduledItem right)
        {
            return Comparer>.Default.Compare(left, right) > 0;
        }
        /// 
        /// Determines whether one specified ScheduledItem<TAbsolute> object is due after or at the same time of a second specified ScheduledItem<TAbsolute> object.
        /// 
        /// The first object to compare.
        /// The second object to compare.
        /// true if the DueTime value of left is later than or simultaneous with the DueTime value of right; otherwise, false.
        /// This operator provides results consistent with the IComparable implementation.
        public static bool operator >=(ScheduledItem left, ScheduledItem right)
        {
            return Comparer>.Default.Compare(left, right) >= 0;
        }
        #endregion
        #region Equality
        /// 
        /// Determines whether two specified ScheduledItem<TAbsolute, TValue> objects are equal.
        /// 
        /// The first object to compare.
        /// The second object to compare.
        /// true if both ScheduledItem<TAbsolute, TValue> are equal; otherwise, false.
        /// This operator does not provide results consistent with the IComparable implementation. Instead, it implements reference equality.
        public static bool operator ==(ScheduledItem left, ScheduledItem right)
        {
            return object.ReferenceEquals(left, right);
        }
        /// 
        /// Determines whether two specified ScheduledItem<TAbsolute, TValue> objects are inequal.
        /// 
        /// The first object to compare.
        /// The second object to compare.
        /// true if both ScheduledItem<TAbsolute, TValue> are inequal; otherwise, false.
        /// This operator does not provide results consistent with the IComparable implementation. Instead, it implements reference equality.
        public static bool operator !=(ScheduledItem left, ScheduledItem right)
        {
            return !(left == right);
        }
        /// 
        /// Determines whether a ScheduledItem<TAbsolute> object is equal to the specified object.
        /// 
        /// The object to compare to the current ScheduledItem<TAbsolute> object.
        /// true if the obj parameter is a ScheduledItem<TAbsolute> object and is equal to the current ScheduledItem<TAbsolute> object; otherwise, false.
        public override bool Equals(object obj)
        {
            return object.ReferenceEquals(this, obj);
        }
        /// 
        /// Returns the hash code for the current ScheduledItem<TAbsolute> object.
        /// 
        /// A 32-bit signed integer hash code.
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        #endregion
        /// 
        /// Cancels the work item by disposing the resource returned by InvokeCore as soon as possible.
        /// 
        public void Cancel()
        {
            _disposable.Dispose();
        }
        /// 
        /// Gets whether the work item has received a cancellation request.
        /// 
        public bool IsCanceled
        {
            get { return _disposable.IsDisposed; }
        }
    }
    /// 
    /// Represents a scheduled work item based on the materialization of an IScheduler.Schedule method call.
    /// 
    /// Absolute time representation type.
    /// Type of the state passed to the scheduled action.
    public sealed class ScheduledItem : ScheduledItem
        where TAbsolute : IComparable
    {
        private readonly IScheduler _scheduler;
        private readonly TValue _state;
        private readonly Func _action;
        /// 
        /// Creates a materialized work item.
        /// 
        /// Recursive scheduler to invoke the scheduled action with.
        /// State to pass to the scheduled action.
        /// Scheduled action.
        /// Time at which to run the scheduled action.
        /// Comparer used to compare work items based on their scheduled time.
        ///  or  or  is null.
        public ScheduledItem(IScheduler scheduler, TValue state, Func action, TAbsolute dueTime, IComparer comparer)
            : base(dueTime, comparer)
        {
            if (scheduler == null)
                throw new ArgumentNullException("scheduler");
            if (action == null)
                throw new ArgumentNullException("action");
            _scheduler = scheduler;
            _state = state;
            _action = action;
        }
        /// 
        /// Creates a materialized work item.
        /// 
        /// Recursive scheduler to invoke the scheduled action with.
        /// State to pass to the scheduled action.
        /// Scheduled action.
        /// Time at which to run the scheduled action.
        ///  or  is null.
        public ScheduledItem(IScheduler scheduler, TValue state, Func action, TAbsolute dueTime)
            : this(scheduler, state, action, dueTime, Comparer.Default)
        {
        }
        /// 
        /// Invokes the scheduled action with the supplied recursive scheduler and state.
        /// 
        /// Cancellation resource returned by the scheduled action.
        protected override IDisposable InvokeCore()
        {
            return _action(_scheduler, _state);
        }
    }
}