using System;

using CommonHelpers;

namespace TypeInColor {

//**************************************************************************************************
static partial class Program {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="HAppFailureException" />
public static void Run(string[] args)
{
   const int expectedParamCount = 2;

   int argCount = args.Length;
   if ( argCount == 0 )
   {
      PrintSyntax();
      return;
   }
   if ( argCount != expectedParamCount )
      throw HAppFailureExceptionCreator.Create(
         "Incorrect number of command-line arguments.\n"+
         "Exactly {0} parameters are expected.\n"+
         "Look syntax (TypeInColor without args).",
         expectedParamCount );
   for (int i=0; i<argCount; i++)
      CheckParameterNormality( args[i], i );

   var colorStr = HString.EmptyAsNull( GetArgumentValue( args, ParamIdx_Color ) );
   if ( colorStr != null )
      if ( ParseColor(colorStr,out ConsoleColor color) )
         _Color_ = color;
      else
         throw CreateParameterError( ParamIdx_Color,
            "<ColorValueOrName>-parameter has invalid value." );

   _FilePath_ = GetArgumentValue( args, ParamIdx_File );
   if ( _FilePath_ == null )
      throw CreateParameterError( ParamIdx_File,
         "<FilePath>-parameter is required." );

   try { PerformTyping(); }
   catch (OperationFailedException e) {
      throw HAppFailureExceptionCreator.Create( e ); }
}

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

/// <exception cref="ArgumentNullException" />
/// <exception cref="IndexOutOfRangeException" />
[HOptionalResult]
static string GetArgumentValue(string[] args,int paramIdx)
{
   HArgChecking.VerifyNotNull( args );
   return HString.EmptyAsNull( args[ paramIdx ] );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // Program
//**************************************************************************************************

} // TypeInColor
