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

namespace CommonHelpers {

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

public HConsole Console
   { get { return _Console_; } }

public ConsoleColor? ForegroundColor
   { get { return _ForegroundColor_; } }
public ConsoleColor? BackgroundColor
   { get { return _BackgroundColor_; } }

public ConsoleColor? PreviousForegroundColor
   { get { return _PreviousForegroundColor_; } }
public ConsoleColor? PreviousBackgroundColor
   { get { return _PreviousBackgroundColor_; } }

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

/// <exception cref="ArgumentNullException" />
public HConsoleColorRetention(HConsole console,ConsoleColor? newForegroundColor)
   :this ( console, newForegroundColor, null )
{}

/// <exception cref="ArgumentNullException" />
public HConsoleColorRetention( HConsole console,
                                ConsoleColor? newForegroundColor,
                                ConsoleColor? newBackgroungColor )
{
   HArgChecking.VerifyNotNull( console );
   _Console_ = console;
   if ( Console.IsInNativeVisualMode )
   {
      _PreviousForegroundColor_ = System.Console.ForegroundColor;
      _PreviousBackgroundColor_ = System.Console.BackgroundColor;
   }
   else
      if ( Console.ReliesOnProxy )
         _PreviousForegroundColor_ = Console.ProxyForegroundColor;
   SetColors( _ForegroundColor_=newForegroundColor, _BackgroundColor_=newBackgroungColor );
   ValidateDisposable();
}

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

protected override void OnDispose(bool synchronous)
{
   SetColors( _PreviousForegroundColor_, _PreviousBackgroundColor_ );
}

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

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

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

void SetColors(ConsoleColor? foregroundColor,ConsoleColor? backgroundColor)
{
   if ( Console.IsInNativeVisualMode )
   {
      if ( foregroundColor.HasValue )
         System.Console.ForegroundColor = foregroundColor.Value;
      if ( backgroundColor.HasValue )
         System.Console.BackgroundColor = backgroundColor.Value;
   }
   else
      if ( Console.ReliesOnProxy && foregroundColor.HasValue )
         Console.ProxyForegroundColor = foregroundColor.Value;
}

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

readonly HConsole
   _Console_;
readonly ConsoleColor?
   _ForegroundColor_, _BackgroundColor_;
ConsoleColor?
   _PreviousForegroundColor_, _PreviousBackgroundColor_;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HConsoleColorRetention
//**************************************************************************************************

} // CommonHelpers
