using System;
using System.Linq;

using CommonHelpers;

namespace SqlCatPP {

//**************************************************************************************************
static class AdditionalParams {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

public static class Names
{
   public const string
      InsertSetVarComments       = nameof(InsertSetVarComments),
      UnfoldEnvironmentVars      = nameof(UnfoldEnvironmentVars),
      ProcessDirectives          = nameof(ProcessDirectives),
      OrderListErrorAsWarn       = nameof(OrderListErrorAsWarn),
      AllowEmptyResult           = nameof(AllowEmptyResult),
      DefaultInputEncodingUTF8   = nameof(DefaultInputEncodingUTF8),
      CheckInputForANSI          = nameof(CheckInputForANSI),
      OutputAsANSI               = nameof(OutputAsANSI),
      OutputFolderPath           = nameof(OutputFolderPath),
      OutputFileName             = nameof(OutputFileName),
      Generated_GenerationOn              = "Generated.GenerationOn",
      Generated_FolderPath                = "Generated.FolderPath",
      Generated_CleanAllSQL               = "Generated.CleanAllSQL",
      Generated_OrderedWithCleanup        = "Generated.OrderedWithCleanup",
      Generated_OrderHeadingChar          = "Generated.OrderHeadingChar",
      Generated_EmbedFileNamesToOrder     = "Generated.EmbedFileNamesToOrder",
      Generated_RestoreDateIfSameContent  = "Generated.RestoreDateIfSameContent";
}
public static class DefaultValues
{
   public const bool
      InsertSetVarComments       = true,
      UnfoldEnvironmentVars      = true,
      ProcessDirectives          = false,
      OrderListErrorAsWarn       = false,
      AllowEmptyResult           = false,
      DefaultInputEncodingUTF8   = true,
      CheckInputForANSI          = false,
      OutputAsANSI               = false;
   public const bool
      Generated_GenerationOn              = false,
      Generated_CleanAllSQL               = false,
      Generated_OrderedWithCleanup        = false;
   public const char
      Generated_OrderHeadingChar          = '0';
   public const bool
      Generated_EmbedFileNamesToOrder     = false,
      Generated_RestoreDateIfSameContent  = false;
}

//**************************************************************************************************

public static string NamePrefix
{
   get { return SqlCatPP_Info.AssemblyName+"."; }
}

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

/// <exception cref="OperationFailedException" />
public static bool ObtainStr(string name,ref string value)
{
   string str = EnvironmentVariableValue( name );
   if ( string.IsNullOrWhiteSpace( str ) )
      return false;
   value = str;
   return true;
}

/// <exception cref="OperationFailedException" />
public static bool ObtainChar(string name,ref char value)
{
   string str = EnvironmentVariableValue( name );
   if ( string.IsNullOrWhiteSpace( str ) )
      return false;
   if ( str.Length != 1 )
      throw AdditionalParamObtainmentError( name );
   value = str[ 0 ];
   return true;
}

/// <exception cref="OperationFailedException" />
public static bool ObtainFlag(string name,ref bool value)
{
   string str = EnvironmentVariableValue( name );
   if ( string.IsNullOrWhiteSpace( str ) )
      return false;
   if ( str == "1" ) value = true;
   else
      if ( str == "0" ) value = false;
      else throw AdditionalParamObtainmentError( name );
   return true;
}

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

/// <exception cref="OperationFailedException" />
public static void VerifyNames()
{
   foreach (object keyObject in Environment.GetEnvironmentVariables().Keys)
   {
      var substr = keyObject as string;
      if ( !string.IsNullOrEmpty( substr ) &&
           substr.StartsWith( NamePrefix, StringComparison.InvariantCultureIgnoreCase ) )
         VerifyNameSupported( substr );
   }
}

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

static string ComposeName(string name)
{
   return NamePrefix + name;
}

static OperationFailedException AdditionalParamObtainmentError(string name)
{
   return OpFailedExceptionCreator.Create(
      "Incorrect value of additional parameter (environment variable): \"{0}\" (look syntax).",
      ComposeName( name ) );
}

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

/// <exception cref="OperationFailedException" />
static void VerifyNameSupported(string substr)
{
   var name = substr.Substring( NamePrefix.Length );
   if ( !Array.Exists( _SupportedParameters_, nm => HString.Equals( nm, name, true ) ) )
      throw OpFailedExceptionCreator.Create(
         "Unknown/unsupported additional parameter (environment variable): \"{0}\" (look syntax).",
         ComposeName( name ) );
}

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

static string EnvironmentVariableValue(string name)
{
   string str = Environment.GetEnvironmentVariable( ComposeName( name ) );
   return str != null ? str.Trim() : null;
}

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

static readonly string[]
   _SupportedParameters_ = new string[]
   {
      Names.InsertSetVarComments,
      Names.UnfoldEnvironmentVars,
      Names.ProcessDirectives,
      Names.OrderListErrorAsWarn,
      Names.AllowEmptyResult,
      Names.DefaultInputEncodingUTF8,
      Names.CheckInputForANSI,
      Names.OutputAsANSI,
      Names.OutputFolderPath,
      Names.OutputFileName,
      Names.Generated_GenerationOn,
      Names.Generated_FolderPath,
      Names.Generated_CleanAllSQL,
      Names.Generated_OrderedWithCleanup,
      Names.Generated_OrderHeadingChar,
      Names.Generated_EmbedFileNamesToOrder,
      Names.Generated_RestoreDateIfSameContent
   };

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // AdditionalParams
//**************************************************************************************************

} // SqlCatPP
