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

using WF = System.Windows.Forms;

using CommonHelpers;

namespace WFX {

//**************************************************************************************************
partial class AppWaiting {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public AppWaiting(): this(AppWaitingStatus.Neutral)
{}

public AppWaiting([HOptional] string caption)
   :this( new AppWaitingStatus(caption) )
{}

public AppWaiting(string captionFmt,params object[] parameters)
   :this( string.Format(captionFmt,parameters) )
{}

public AppWaiting([HOptional] AppWaitingStatus status)
   :this( status, false )
{}

public AppWaiting(bool isSuspended,bool isSuppressedForBackoff=true)
   :this( null, isSuspended )
{
   _SuppressBackOff_ = isSuppressedForBackoff;
}

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

public AppWaitingStatus InitialStatus
{
   get
   {
      return Dispatcher.Synchronize(delegate{
         VerifyActive(); return _InitialStatus_;
      });
   }
}

public string InitialCaption
{
   get
   {
      return Dispatcher.Synchronize(delegate{
         VerifyActive(); return _InitialStatus_.Caption;
      });
   }
}

public AppWaitingStatus PreviousStatus
{
   get
   {
      return Dispatcher.Synchronize(delegate{
         VerifyActive(); return _PreviousStatus_;
      });
   }
}

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

public void RevertStatusToInitial()
{
   Dispatcher.Synchronize(delegate{
      DisplayStatus( _InitialStatus_ );
   });
}

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

protected override void OnDispose(bool synchronous)
{
   Dispatcher.Synchronize(delegate{
      if (!_SuppressBackOff_)
      {
         VerifyActive();
         _IsInDispose_ = true;
         try
            { DisplayStatus(_PreviousStatus_); }
         finally
            { _IsInDispose_ = false; }
         _CurrentInstance_ = _PreviousInstance_;
      }
   });
}

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

AppWaiting([HOptional] AppWaitingStatus status,bool isSuspended)
{
   Dispatcher.Synchronize(delegate{
      VerifyActive();
      AppWaitingStatus stat;
      if (status!=null)
         stat = status;
      else
      {
         stat = CurrentStatus.Duplicate();
         stat._IsSuspended_ = isSuspended;
      }
      _PreviousStatus_ = _CurrentStatus_;
      _PreviousInstance_ = HOps.Exchange(ref _CurrentInstance_,this);
      SetStatus( stat!=AppWaitingStatus.Neutral ? stat : CurrentStatus );
      _InitialStatus_ = _CurrentStatus_;
      ShowStatus();
   });
   ValidateDisposable();
}

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

AppWaitingStatus
   _InitialStatus_,
   _PreviousStatus_;
AppWaiting
   _PreviousInstance_;
bool
   _SuppressBackOff_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // AppWaiting
//**************************************************************************************************

} // WFX
