//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;

namespace CommonHelpers {

//**************************************************************************************************
/// <summary>
/// </summary>
[HSyncOnlyDisposable]
public partial class HConsoleInputIdlePollingContext: HDisposable {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public HConsoleInputIdlePollingContext( HConsole console,
                                         HConsoleInputIdlePollingCallback callback )
   :this( console, callback, HConsole.InputIdlePollingDefaultInterval )
{}

public HConsoleInputIdlePollingContext( HConsole console,
                                         HConsoleInputIdlePollingCallback callback,
                                         int interval )
{
   HArgChecking.VerifyNotNulls( console, callback );
   if (interval<0)
      throw new ArgumentOutOfRangeException();
   if (console.InputIdlePollingContext!=null)
      throw new InvalidOperationException(
         "Input idle polling context is already active." );
   lock (_InstanceSyncRoot_)
   {
      _Console_  = console;
      _Callback_ = callback;
      _Interval_ = interval;
   }
   ValidateDisposable();
   ConsoleInstance.SetInputIdlePollingContext( this );
}

//--------------------------------------------------------------------------------------------------

public HConsole ConsoleInstance
{
   get
   {
      return _Console_;
   }
}

public HConsoleInputIdlePollingCallback Callback
{
   get
   {
      return _Callback_;
   }
}

public int Interval
{
   get
   {
      return _Interval_;
   }
}

//==================================================================================================

protected override void OnDispose(bool synchronous)
{
   ConsoleInstance.SetInputIdlePollingContext( null );
}

//==================================================================================================

static HConsoleInputIdlePollingContext()
{
   CommonHelpers_Console.Access();
}

//==================================================================================================

HConsole
   _Console_;
HConsoleInputIdlePollingCallback
   _Callback_;
int
   _Interval_;

readonly object
   _InstanceSyncRoot_ = new object();

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HConsoleInputIdlePollingContext
//**************************************************************************************************

} // CommonHelpers
