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

using CommonHelpers;

namespace WFX {

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

public static bool IsActive
{
   get
   {
      HProgram.VerifyInMainThread();
      return _ActiveInstance_ != null && _ActiveInstance_._IsActive_;
   }
}

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

public ForegroundOperation([HOptional] string title)
{
   HProgram.VerifyInMainThread();
   AppWaiting.Activate( title );
   Initialize();
}

public ForegroundOperation([HOptional] string title,[HOptional] string caption)
{
   HProgram.VerifyInMainThread();
   AppWaiting.Activate( title, caption );
   Initialize();
}

public ForegroundOperation([HOptional] string title,string captionFmt,params object[] parameters)
{
   HProgram.VerifyInMainThread();
   AppWaiting.Activate( title, captionFmt, parameters );
   Initialize();
}

public ForegroundOperation([HOptional] string title,AppWaitingStatus status)
{
   HProgram.VerifyInMainThread();
   AppWaiting.Activate( title, status );
   Initialize();
}

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

public static ForegroundOperation WithData([HOptional] string title,[HOptional] object data)
{
   HProgram.VerifyInMainThread();
   AppWaiting.ActivateWithData( title, data );
   return new ForegroundOperation();
}

public static ForegroundOperation WithData( [HOptional] string title,
                                            [HOptional] string caption,
                                            [HOptional] object data )
{
   HProgram.VerifyInMainThread();
   AppWaiting.ActivateWithData( title, caption, data );
   return new ForegroundOperation();
}

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

public static void DetachAppWaitingSuppressBackOff()
{
   DetachAppWaitingSuppressBackOff( false );
}

public static void DetachAppWaitingSuppressBackOff(bool revertStatusToRoot)
{
   if ( revertStatusToRoot )
      AppWaiting.RevertStatus( true );
   if ( _ActiveInstance_ != null )
   {
      _ActiveInstance_._IsActive_ = false;
      _ActiveInstance_ = null;
   }
   AppWaiting.SuppressBackOff();
}

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

public static bool CompleteActive()
{
   HProgram.VerifyInMainThread();
   if ( !IsActive ) return false;
   _ActiveInstance_.Dispose();
   return true;
}

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

protected override void OnDispose(bool synchronous)
{
   HProgram.VerifyInMainThread();
   if ( _IsActive_ )
   {
      AppWaiting.Deactivate();
      _ActiveInstance_ = null;
   }
}

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

ForegroundOperation()
{
   Initialize();
}

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

void Initialize()
{
   HProgram.VerifyInMainThread();
   if ( _ActiveInstance_ !=null )
      throw new InvalidOperationException(
         "Another foreground operation is active." );
   _IsActive_ = true;
   _ActiveInstance_ = this;
   ValidateDisposable();
}

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

static ForegroundOperation()
{
   WFX_Library.Access();
}

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

bool _IsActive_;

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

static ForegroundOperation
   _ActiveInstance_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // ForegroundOperation
//**************************************************************************************************

} // WFX
